Data Types

Introduction to Data Types and Variables in JS

A variable could store any type of data. It could be one of the primitive type or a complex type of a class.

CODE1

Javascript allows you to define dynamic type. You can define first as string data type than it can be assigned to a different data type value (in the below example it is number data type value)

CODE2

Let's review the data types.

Undefined

Undefined data type has only one value which is undefined. Variable is assigned to undefined value when it is not set to value after it is defined

CODE3

Null

Null data type has only one value which is null. Javascript returns true if you compare Null and undefined equavialent

A number (integer)

The following code is an example of integer data type

CODE4

A number (float)

The following code is an example of float data type

CODE5

NaN

Nan means not a number. It is a data type which states it is an invalid number.

CODE6

Any operation with Nan datatype returns as NaN

CODE7

It returns as false when you compare any value with NaN even including itself

CODE8

A string

String data type containe zero or more characters. Strings could be defined with the following charachters ", ', ` . There is no difference on what you used when you define the string.

CODE9

To add number data type into a string variable ends up with concanating variables with string type.

CODE10

Embeding an expression


var expression1 = `2 + 3 equals to ${2 + 3}`
The expression inside ${…} is executed and the result becomes a part of the string. We can put anything in there: a variable like name or an arithmetical expression like 2 + 3 or something more complex. Please note that this can only be done in backticks. Other quotes don’t have this embedding functionality! alert( "the result is ${1 + 2}" ); // the result is ${1 + 2} (double quotes do nothing)

A boolean

The boolean type has only two values. It is whether true or false.

CODE10

Boolean values also come as a result of comparisons:
var isGreater = 4 > 1;
console.log( isGreater ); // true (the comparison result is "yes")

CODE11