Variables in JavaScript

JavaScript has 3 basic data types:

1. Number : This includes both decimal (fraction) and  whole numbers

2. String : String of characters, digits, special characters (in single or double quotes)

3. Boolean : True / False

There are 2 additional data types in JavaScript

1. BigInt : For really large numbers (beyond safe integer limit)

2. Symbol: For storing symbols (for example: $)

Variable Declaration ('let' Keyword)

Keyword ‘let’ is used to declare variables in JavaScript. 

				
					//Whole Number
let a = 100

//Decimal Number
let b = 24.99

//String
let str1 = "I love Node JS"

//Boolean
let isItFun = true

//BigInt (using constructor)
let bigNumber = BigInt(12345678901234567890123345456)

//BigInt (by appending 'n' at the end)
let bigNumber = 12345678901234567890123345456n

//Important note: BigInt can be created by using 'BigInt' constructor or by appending 'n' at the end of the number.


//Symbol
let s = Symbol('$')

				
			

'var' Keyword

Keyword ‘var’ can also be used to declare variables in JavaScript.

The main difference in between ‘let’ and ‘var’ is that using ‘var’ we can re-declare a variable. See following example:

				
					//Declare 'a' as Number
var a = 100
.
.
//Re-declare 'a' as String
var a = "This is so much fun."

Note: This is fine with 'var' but not allwoed with 'let'
				
			

Primitive Data Type Values in JavaScript

TypeDeclarationSizeRange
Numberlet a = 10064 bitsFrom -2^53 – 1 to 2^53 – 1
Stringlet str = “Learn It Easy”16 bitsString of any upper / lower case character and/or digit and/or special character
Booleanlet b = true1 bitTrue / False

Undefined vs Null

Undefined

				
					//Undefined is used as a Type in JavaScript. 
//When we declare a variable without any assignment and use it without any assigned value then it is treated as 'Undefined'.

let a

console.log(a)
console.log(typeof(a))

Output:-> 
undefined
undefined
				
			

Null

				
					//When an object contains nothing, we say it is a 'null'.

let obj = null

console.log(obj)
console.log(typeof(obj))

Output:->
null
object
				
			

Assignment

  • Declare variables of several data types in JS and print their values and data types in the console.
  • Can we change data type of a variable in JS by simply changing the value content of that variable? (Yes ? / No ?)