Please Click on this link to access the resource folder for this lesson.
Data Types
A value in JavaScript is always of a certain type. e.g, string, number.
There are eight (8) basic data types in JavaScript, these are:
number
bigInt
string
boolean
null
undefined
object
symbol
We can put any type in a variable. For example, a variable can at one moment be a string and then store a number: 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.
Data types (number)
The number type represents both integer and floating point numbers.
The following operations work on numbers: multiplication *, division /, addition +, subtraction -.
There are “special numeric values†which also belong to this data type: Infinity, -Infinity and NaN.
Infinity represents the mathematical Infinity ∞. e.g divide by 0
NaN represents a computational error. It is a result of an incorrect or an undefined mathematical operation, for instance: console.log(“name” / 2)
NaN is sticky. Any further mathematical operation on NaN returns NaN
So, if there’s a NaN somewhere in a mathematical expression, it propagates to the whole result (there’s only one exception to that: NaN ** 0 is 1).
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.
Data types (BigInt)
The bigInt type represents extremely large or small integers which cannot be represented by the number type.
The maximum and minimum size for a number type is (253-1) and -(253-1)
This number type is enough for many purpose, so the bigInt type is rarely used.
A BigInt is created by adding n to the end of a number or using the BigInt constructor