Introduction
Namaste, In this blog post I will discuss the following topics
Primitive data types in JavaScript
Equality behaviour
Complete source code
Primitive Data Types In JavaScript
A primitive data type is a fundamental type that is used to hold the data in the purest form, so that
A conversion is not required when used in the computation
To maintain data integrity
There are 7 primitive types in JS
- undefined: When a variable is declared but is not initialized. A variable can also be initialized to undefined.
let mydata;
console.log("type: ", typeof mydata, " value: ", mydata);
// result - type: undefined value: undefined
let mydata2=2;
mydata2=undefined; // valid initialization
console.log("type: ", typeof mydata2, " value: ", mydata2);
// result - type: undefined value: undefined
number: Can hold integer and float values.
let mydata=30; console.log("type:", typeof mydata, " value:", mydata); // result - type: number value: 30 mydata = 56.78; console.log("type:", typeof mydata, " value:", mydata); // result - type: number value: 56.78
boolean: It is used for holding 2 states i.e. true or false.
let mydata=false; console.log("type:", typeof mydata, " value:", mydata); // result - type: boolean value: false mydata = true; console.log("type:", typeof mydata, " value:", mydata); // result - type: boolean value: true
bigint: It is used for holding large integers. The value is suffixed using 'n'.
let mydata=1000000000000000000000000000000000000000000000000000000000000000000000000000000000n; console.log("type:", typeof mydata, " value:", mydata); // mydata=100.89899n; // error
null: It is used for holding a 'null' value i.e. value that is not applicable. The data type for null is identified as an object (bug in Javascript)
let mydata=null; console.log("type:", typeof mydata, " value:",mydata); // result - type : object value: null
string: It is used to hold a sequence of characters.
let mydata="Namaste Javascript"; console.log("type:", typeof mydata, " value:", mydata); // result - type: string value: Namaste Javascript mydata= "100"; console.log("type:", typeof mydata, " value:", mydata); // result - type: string value: 100
symbol: It is used to hold unique values. The values are immutable. Symbols can be used as property keys in an object.
let sym = Symbol("EmpNo"); let sym2 = Symbol("EmpName"); console.log("Compare", sym === sym2); //Result - Compare false let employee = { [sym] : 100, [sym2] : "Ram" }; console.log("object", employee); //Result - object {Symbol(EmpNo): 100, Symbol(EmpName): 'Ram'}
Equality Behaviour
The primitive types are compared by value, not by reference.
let mydata= 100;
let mydata2 = 200;
console.log("comapare-number", mydata === mydata2);
//result - comapare-number false
mydata="ram";
mydata2="ram";
console.log("compare string", mydata === mydata);
//result - compare string true
mydata=false;
mydata2=false;
console.log("compare boolean", mydata === mydata);
//result - compare boolean true
Complete Source Code
Program 1
//Objective - Program to demonstrate primitive data types in JavaScript
//Author - Mahavir DS Rathore
let mydata;
console.log("type: ", typeof mydata, " value: ", mydata);
// result - type: undefined value: undefined
mydata=30;
console.log("type:", typeof mydata, " value:", mydata);
// result - type: number value: 30
mydata = 56.78;
console.log("type:", typeof mydata, " value:", mydata);
// result - type: number value: 56.78
mydata=false;
console.log("type:", typeof mydata, " value:", mydata);
// result - type: boolean value: false
mydata = true;
console.log("type:", typeof mydata, " value:", mydata);
// result - type: boolean value: true
mydata=1000000000000000000000000000000000000000000000000000000000000000000000000000000000n;
console.log("type:", typeof mydata, " value:", mydata);
// mydata=100.89899n; // error
mydata=null;
console.log("type:", typeof mydata, " value:",mydata);
// result - type : object value: null
mydata="Namaste Javascript";
console.log("type:", typeof mydata, " value:", mydata);
// result - type: string value: Namaste Javascript
mydata= "100";
console.log("type:", typeof mydata, " value:", mydata);
// result - type: string value: 100
let sym = Symbol("EmpNo");
let sym2 = Symbol("EmpName");
console.log("Compare", sym === sym2);
//Result - Compare false
let employee = {
[sym] : 100,
[sym2] : "Ram"
};
console.log("object", employee);
//Result - object {Symbol(EmpNo): 100, Symbol(EmpName): 'Ram'}