In JavaScript, a value is set to 1 of 7 primitive data types:
Undefined
(a variable with no defined value -- undefined
is the only value)Null
(a single null value -- null
is the only value)Boolean
(logical value true
or false
)Number
(includes Infinity
and not-number NaN
)BigInt
(an integer value larger than 2^53 -1)String
(textual data)Symbol
(a unique and immutable primitive new to ES6/2015)Everything else falls to the 8th data type:
Object
(a complex data structure, including arrays and functions)Note on function
: Function's data type is object. However when running typeof
operator to a function, the result is "function". This is an error in JavaScript. It should return "object".
JavaScript is dynamically typed programming language. Data types exist, but variables are not bound to it. Variables are declared without declaring the type. And the type can easily changed based on the content.
Number is limited within -(253-1)
to (253-1)
. Outside this, we can use BigInt
.
A BigInt
value is ceated by appending n
to the end of an integer literal, or by calling the function BigInt()
Mathematical operations works for BigInt too, but can't mix other type (niculding number) and BigInt within an operation
Further reading: Javascipt.Info: BigInt
const bigNum = 1234567890123456789012345678901234567890n; 1n + 2n // 3n typeof (1n + 2n) // 'bigint' 1n + 2 // error
Variables with string data type is created by using quotes. Type of quotes in JS:
Using backticks, it's possible to embed variables and expressions inside a string by wrapping them in ${...}
. With simple quotes, this can't be done. You'l have to use a lot of concatenations.
let name="Kitten"; alert(`Hello, ${name}!`); // Alert: Hello, Kitten! alert("Hello, " + name + "!"); // Alert: Hello, Kitten! alert(`The result is ${1 + 2}`);
There is no "char" type for a single chaacter string like in C and Java. A string can be empty, 1 character, or many.
Further reading: Javascipt.Info: String
The boolean logical type only has 2 values:
true
(1)false
(0)
Boolean(0); // false
Boolean(NaN); // false
Boolean(123); // true, all numbers except 0 and NaN
Boolean(Infinity); // true
Boolean(""); // false
Boolean("123"); // true
Boolean("true"); // true
Boolean("false"); // true
Boolean(Number(true)); // true
Boolean(Number(false)); // false
Boolean(Number(true)); // true
Boolean(Number(false)); // true
Number(true); // 1 Number(false); // 0 BigInt(true); // 1 BigInt(false); // 0 String(true); // "true" String(false); // "false"
The special value null
belongs to its own type: null
.
And vice versa, the special type null
only has 1 possible value: null
.
Value null
in JavaScript represents "nothing", "empty", or "value unknown". It's not a "reference to a non-existing object" or a "null pointer" like in some other language.
let name = null; // The code states that name is unknown name; // null typeof name; // "object"
typeof(null)
returns "object"
. This is an error in the language. The actual type is null, not object.
The special value undefined
belongs to its own type: undefined
.
And vice versa, the special type undefined
only has 1 possible value: undefined
.
When a variable has undefined
type?
undefined
is assigned to a variablelet name; name; // undefined name = "Kitty" name; // "Kitty" name = undefined; name; // undefined
For good practise, use null
to assign an empty or unknown value to a variable. Reserve undefined
as a default initial value fo unassigned things.
Any other types of data are called primitive because their values can contain only a single thing. In contrast, object
is used to store collections of data and more complex entities.
Read more: JavaScript.Info - Object
Data type symbol
is used to create unique identifiers for objects
For checking the data type of a variable, use typeof
operator. The result of typeof
is a string containing the data type name as the value.
typeof undefined // "undefined" typeof 0 // "number" typeof 10n // "bigint" typeof true // "boolean" typeof "foo" // "string" typeof Symbol("id") // "symbol" typeof Math // "object" (Math is an built-in object) typeof null // "object" (It's actually null) typeof alert // "function" (there is no special function data type, it belongs to object data type)
Since typeof
is an operator, instead of a function, no parentheses then ()
is needed. When typeof()
syntax is used, the ()
is mathematical grouping of what we want to check. If it's only consist of 1 argument, then ()
is not needed.
typeof ("a" + 2) // 'string' typeof ("a" - 2) // 'number'