JavaScript Data Types

JavaScript Data Types

Data has types now? What?

Any kind of variable, function, array or object that you use while programming in JS falls into one of the following categories.

image.png

Numbers

image.png

Operations that can be performed on numbers:

//Addition
12+45

//Subtraction
12-45

//Multiplication
12*45

//Division - returns the quotient
45/12

//Modulo - returns the remainder
45%12

NaN

Not an Number. It is a numerical value that represents something that is not...quite a number.

//NaN
0/0
123 + 0/0

Boolean

image.png

let gameOver = false;  //0

let isStillAlive = true;  //1

const riverFull = true;  //1

Null

'null' is used to indicate the intentional absence of a value

To check of a value is 'null', use the '===' strict equality operator. This operator not only checks value but also type of values.

value === null

'null' has a 'falsey' value. This means that when used in conditionals it will be coerced into 'false'.

The typeof null return 'object'.

null vs. undefined

image.png

If you declare a variable without initializing it, the variable will evaluate to 'undefined'.

'null' represents a missing object.

null === undefined; //false

Variables

Variables can be thought of a labelled container for a value. The label specifies the name of your variable while the container holds the data. This container later on can be used or changed.

image.png

let livesLeft = 9;

Variable declared with 'const' cannot be changed at any time during your program.

const pi = 3.14;

Variables can change Type

let livesLeft = 9;  //its a number
let livesLeft = 'Cat';  //its now a string
let livesLeft = true;  //its now a boolean