JS Fundamentals - Data types Flashcards
8 basic data types
There are eight basic data types in JavaScript. Here, we’ll cover them in general and in the next chapters we’ll talk about each of them in detail.
We can put any type in a variable. For example, a variable can at one moment be a string and then store a number:
// no error let message = "hello"; message = 123456;
Programming languages that allow such things, such as JavaScript, are called “dynamically typed”, meaning that there exist data types, but variables are not bound to any of them.
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.
We can get it as a result of division by zero:
alert( 1 / 0 ); // Infinity
Or just reference it directly:
alert( Infinity );
what is NaN
It is a computational error. It is a result of an incorrect or an undefined mathematical operation.
NaN is sticky. Any further operation on NaN returns NaN:
alert( “not a number” / 2 + 5 ); // NaN
Why JS is safe in doing mathematical operations
Doing maths is “safe” in JavaScript. We can do anything: divide by zero, treat non-numeric strings as numbers, etc.
The script will never stop with a fatal error (“die”). At worst, we’ll get NaN as the result.
BigInt
In JavaScript, the “number” type cannot represent integer values larger than (253-1) (that’s 9007199254740991), or less than -(253-1) for negatives. It’s a technical limitation caused by their internal representation.
For most purposes that’s quite enough, but sometimes we need really big numbers, e.g. for cryptography or microsecond-precision timestamps.
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;
Right now, BigInt is supported in Firefox/Chrome/Edge/Safari, but not in IE.
BigInt exact preciseness is not given in the documents.
String
let str = "Hello"; let str2 = 'Single quotes are ok too'; let phrase = `can embed another ${str}`;
no char datatype exists
In some languages, there is a special “character” type for a single character. For example, in the C language and in Java it is called “char”.
In JavaScript, there is no such type. There’s only one type: string. A string may consist of zero characters (be empty), one character or many of them.
boolean
The boolean type has only two values: true and false
The null value
The special null value does not belong to any of the types described above.
It forms a separate type of its own which contains only the null value:
let age = null; In JavaScript, null is not a “reference to a non-existing object” or a “null pointer” like in some other languages.
It’s just a special value which represents “nothing”, “empty” or “value unknown”.
The code above states that age is unknown.
undefined
The special value undefined also stands apart. It makes a type of its own, just like null.
The meaning of undefined is “value is not assigned”.
If a variable is declared, but not assigned, then its value is undefined:
Technically, it is possible to explicitly assign undefined to a variable
Normally, one uses null to assign an “empty” or “unknown” value to a variable, while undefined is reserved as a default initial value for unassigned things.
Objects
All other types are called “primitive” because their values can contain only a single thing (be it a string or a number or whatever). In contrast, objects are used to store collections of data and more complex entities.
typeof operator
The typeof operator returns the type of the argument. It’s useful when we want to process values of different types differently or just want to do a quick check.
various type of
typeof undefined // “undefined”
typeof 0 // “number”
typeof 10n // “bigint”
typeof true // “boolean”
typeof “foo” // “string”
typeof Symbol(“id”) // “symbol”
typeof Math // “object” (1)
typeof null // “object” (2)
typeof alert // “function” (3)
The result of typeof null is “object”. That’s an officially recognized error in typeof behavior, coming from the early days of JavaScript and kept for compatibility. Definitely, null is not an object. It is a special value with a separate type of its own.