In this post, we are going to learn DataTypes in JavaScript with examples. The datatypes in any language are used to specify which type of data we want to store. First, we will understand how javascript is a dynamic type language. We will also learn about typeof operator.
DataTypes in JavaScript
- Primitive Type
- string
- Number
- Boolean
- Undefined
- null
- Symbol
- Reference Type/Non-primitive type
- object
- Array
JavaScript Dynamic Typing
JavaScript is a loosely typed and dynamic language. The variables in javascript are not associated with any value type. Variable can be assigned or reassigned the value of any type.
The variable is one type when we define, it can change to another type later as in the below example. a is undefined, assign to value 5 change its type to number type, assign string value changes its type to string
Example
let a;
console.log(typeof(a)) //undefined
a = 5;
console.log(typeof(a)) //number
a= "devenum"
console.log(typeof(a)) //string
JavaScript Typeof
The JavaScript Typeof() operator is used to check the type of a variable. It returns a string indicating the type of the operand. We will use it in examples later in this post.
Syntax
typeof opeand
typeof(operand)
Primitive Data Types in JavaScript
1. String Type
The string data type is a collection of characters used to represent the textual data. It is defined by a single quote(”), double quote(” “), backtick character(“) also known as Template string in JavaScript.
Example
let name = 'Dev';
let domain = "Devenum.com"
let greet = `Welcome to devenum
good morning`
console.log(typeof(name))
console.log(typeof(domain))
console.log(typeof(greet))
Output
string
string
string
2. Number Type
The number type represents negative or positive integer and floating-point number between -(2^53 − 1) and 2^53 − 1. Javascript number type also includes +infinity, -infinity, NAN. The NAN represents an invalid number. We can check the largest or smallest safe value of a number by using constant Number.MAX_VALUE
or Number.MIN_VALUE
.
Since ES6(ECMAScript 2015) we can check if the number falls in the double-precision floating-point range by using Number.isSafeInteger()
An integer beyond this range is not safe anymore. It will be a double-precision floating-point approximation of the value.
Example
let count = 98.9019;
let num = -98;
if(Number.isSafeInteger(count))
{
console.log('Precision is safe.')
}
else
{
console.log('Precision is not safe.')
}
const val = 42/0;
console.log(typeof(count)) //number
console.log(typeof(num)) //number
console.log(val) //Infinity
console.log(Number.MAX_VALUE ) //1.7976931348623157e+308
Example :NAN
let mynum = "hell0"/3
console.log(mynum) //NAN
ECMAScript has two built-in numeric types Number and Bigint.
BigInt type
“The BigInt type is a numeric primitive in JavaScript that represents a whole number greater than 2^53 – 1” The bigint is defined by appending n at end of an integer value. By using constant Number.MAX_SAFE_INTEGER we can find out the largest number represented within the number primitive type.
Example :Bigint
const x = 2n ** 53n;
console.log(typeof(x)) //bigint
console.log(Number.MAX_SAFE_INTEGER) //9007199254740991
3. Boolean Type
The boolean data type represents the logical entity. It has two values true or false. It helps us to control program flow using the if-else, switch statement.
Example
var a= true;
if(a)
{
console.log('you are 18')
}
else
{
console.log('you are not 18')
}
//output
you are 18.
4. Undefined type
In JavaScript, if a variable is declared and but no value is initialized to that variable then the value of that variable will be undefined.
JavaScript Example
let a;
console.log(a) //undefined
5. null
The JavaScript null primitive type has only one value null that represents the absence of any object value. It is used for the special value that is unknown or empty.
Example
const mynum = null;
6.symbol
The Symbol primitive datatype was introduced in ES6. The symbol is created by using the symbol function. We can pass the string into a symbol function for debugging and logging use. The symbol always creates unique value.
Example
let val = symbol('Information logged')
let val2 = symbol('Information logged')
console.log(val == val2); //false
Reference Type/Non-primitive type
Object
The object is a reference data type in javascript. An object type is a complex type that stores a collection of properties with the object literal({}) and property are identified by key-value pair. Where key is string and value can be any datatype or including another object.
Example
var fruits = {
"name": "apple", "price": 20, "weight":10, "quantity": 6
}
console.log(fruits.name) //apple
console.log(fruits.price) //20
console.log(typeof(fruits)) //object
Array
An array is an object type in javascript that is used to store multiple elements in a single variable of any data type. Each value in the array is correlated with a numeric index. The Index of the array’s first element is starting with 0. We often use an index to access the elements of an array.
Example
let fruits = [
"apple", "orange", "Banana", "KIWI"
]
for(let j = 0;j < fruits.length;j++)
{
console.log(fruits[j]);
}
//Output:
apple
orange
Banana
KIWI