JS Types

JavaScript Data Types

A value in JavaScript is always of a certain type. For example, a string or a number. There are eight basic data types in JavaScript.

Programming languages that allow you to put any type into a variable, like JavaScript, are called “dynamically typed”. This means that data types exist, but variables are not bound to any of them. A variable can hold a string one moment and a number the next.

Dynamically Typed Example

// No error
let message = "hello";
console.log(typeof message); // "string"

message = 123456; console.log(typeof message); // "number"


1. Number

The number type represents both integer and floating-point numbers.

Besides regular numbers, there are so-called “special numeric values” which also belong to this data type: Infinity, -Infinity, and NaN.

Special Numeric Values

// Division by zero gives Infinity
console.log( 1 / 0 ); // Infinity

// A mathematical error results in NaN console.log( "not a number" / 2 ); // NaN

NaN is "sticky". Any further mathematical operation on NaN returns NaN.

In JavaScript, mathematical operations are "safe". A script will never stop with a fatal error. At worst, you’ll get NaN as the result.


2. BigInt

In JavaScript, the number type cannot safely represent integer values larger than (2^53 - 1). The BigInt type was recently added to the language to represent integers of arbitrary length.

A BigInt value is created by appending n to the end of an integer.

BigInt Example

// The "n" at the end means it's a BigInt
const bigInt = 1234567890123456789012345678901234567890n;

console.log(typeof bigInt); // "bigint"


3. String

A string in JavaScript must be surrounded by quotes. There are three types:

  1. Double quotes: "Hello"
  2. Single quotes: 'Hello'
  3. Backticks: `Hello`

Double and single quotes are “simple” quotes with no functional difference. Backticks are “extended functionality” quotes. They allow us to embed variables and expressions into a string by wrapping them in ${...}. This is called a template literal.

String and Template Literals

let name = "John";

// Embed a variable console.log( Hello, ${name}! ); // Hello, John!

// Embed an expression console.log( the result is ${1 + 2} ); // the result is 3

Note: In JavaScript, there is no special "character" type. A string can have zero, one, or many characters.


4. Boolean (Logical Type)

The boolean type has only two values: true and false. This type is commonly used to store yes/no values. Boolean values also come as a result of comparisons.

Boolean Example

let nameFieldChecked = true; // yes, name field is checked
let isGreater = 4 > 1;

console.log(nameFieldChecked); // true console.log(isGreater); // true (the comparison result is "yes")


5. The "null" Value

The special null value forms a separate type of its own. It’s a special value which represents “nothing”, “empty” or “value unknown”.

null Example

let age = null;
console.log(age); // null

6. The "undefined" Value

The special value undefined also stands apart, making a type of its own. The meaning of undefined is “value is not assigned”. If a variable is declared, but not assigned, then its value is undefined.

undefined Example

let age;
console.log(age); // shows "undefined"

7. Objects and Symbols


The typeof Operator

The typeof operator returns the type of the operand as a string. It’s useful when we want to process values of different types differently.

`typeof` Operator Examples

console.log(typeof undefined);     // "undefined"
console.log(typeof 0);             // "number"
console.log(typeof 10n);           // "bigint"
console.log(typeof true);          // "boolean"
console.log(typeof "foo");         // "string"
console.log(typeof Symbol("id"));  // "symbol"
console.log(typeof Math);          // "object"
console.log(typeof null);          // "object" (an error in the language)
console.log(typeof alert);         // "function"

Note on typeof null: The result of typeof null is "object". This is an officially recognized error in typeof coming from the early days of JavaScript and is kept for compatibility. null is not an object.


Summary

There are 8 basic data types in JavaScript.

Seven primitive data types:

And one non-primitive data type:


Tasks

String quotes

What is the output of the script below?

Task: String Quotes

let name = "Ilya";

console.log( hello ${1} ); // ?

console.log( hello ${"name"} ); // ?

console.log( hello ${name} ); // ?


Exercise

?

What does the typeof null operation return in JavaScript?