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.
// No error let message = "hello"; console.log(typeof message); // "string"message = 123456; console.log(typeof message); // "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.
Infinity represents the mathematical Infinity (∞). It is a special value that’s greater than any number. We can get it as a result of division by zero.NaN stands for "Not-a-Number" and represents a computational error. It is a result of an incorrect or an undefined mathematical operation.// 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
NaNas the result.
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.
// The "n" at the end means it's a BigInt const bigInt = 1234567890123456789012345678901234567890n;console.log(typeof bigInt); // "bigint"
A string in JavaScript must be surrounded by quotes. There are three types:
"Hello"'Hello'`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.
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.
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.
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")
The special null value forms a separate type of its own. It’s a special value which represents “nothing”, “empty” or “value unknown”.
let age = null; console.log(age); // null
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.
let age; console.log(age); // shows "undefined"
object type is special. All other types are called “primitive” because their values can contain only a single thing. In contrast, objects are used to store collections of data and more complex entities.symbol type is used to create unique identifiers for objects.typeof OperatorThe typeof operator returns the type of the operand as a string. It’s useful when we want to process values of different types differently.
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 oftypeof nullis"object". This is an officially recognized error intypeofcoming from the early days of JavaScript and is kept for compatibility.nullis not an object.
There are 8 basic data types in JavaScript.
Seven primitive data types:
number for numbers of any kind.bigint for integer numbers of arbitrary length.string for strings.boolean for true/false.null for unknown values.undefined for unassigned values.symbol for unique identifiers.And one non-primitive data type:
object for more complex data structures.What is the output of the script below?
let name = "Ilya";console.log(
hello ${1}); // ?console.log(
hello ${"name"}); // ?console.log(
hello ${name}); // ?
What does the typeof null operation return in JavaScript?