Vaidikalaya

Data Types In Node


Variables can store different types of data, so the data types define the type of data a variable can store. Node allows different data types. Here’s a detailed explanation of the various data types you can use in Node.js.


Node Data Types:
  1. Number
  2. String
  3. Boolean
  4. Null
  5. Undefined
  6. Symbol
  7. Array
  8. Object

Let's discuss all the types in detail.


1. Number

The Number data type is used to represent both integer and floating-point values. It also handles special numeric values such as Infinity, -Infinity, and NaN (Not-a-Number). The Number data type is versatile and is commonly used for mathematical operations.

let age = 30;
let price = 19.99;
console.log("age: "+age);
console.log("price: "+price);

/*Output
age: 30
price: 19.99
*/

Infinity, -Infinity, and NaN:

console.log(1 / 0);
console.log(-1 / 0);
console.log("hello" / 2);

/*Output
Infinity
-Infinity
NaN
*/

2. String

A string is a sequence of characters enclosed in single quotes ('), double quotes ("), or backticks (`). Strings are immutable, meaning that once a string is created, it cannot be changed, but you can create new strings based on the original.

The String data type in Node.js is essential for handling textual data, performing text manipulations, and interacting with text-based APIs.

let str1="Hello Vaidikalaya";    //double quote string
let str2='Hello Vaidikalaya';   //single quote string
let str3=`Hello Vaidikalaya`;  //template literal string

console.log(str1);
console.log(str2);
console.log(str3);

/*Output
Hello Vaidikalaya
Hello Vaidikalaya
Hello Vaidikalaya
*/

⮚ Template literals allow for embedding expressions and multi-line strings.

let name = 'Vaidikalaya';
let greeting = `Hello, ${name}!`;  // Embedding an expression
console.log(greeting);

/*Output
Hello, Vaidikalaya!
*/

/****Multi Line String****/

let multiLineString = `This is a
multi-line
string.`;
console.log(multiLineString);

/*Output
This is a
multi-line
string.
*/

3. Boolean

The Boolean data type represents logical values and can be either true or false. Booleans are commonly used in control flow statements like if, else, while, and for logical operations to determine the flow of a program based on conditions.

let day='Sunday';
let isWeekEnd = day=='Sunday'?true:false;

console.log(isWeekEnd);

/*Output
true
*/

4. Null

The Null data type represents the intentional absence of any object value. It is used to explicitly indicate that a variable should have no value.

⮚ Null is an assignment value, meaning you can assign null to a variable to explicitly indicate that it should be empty.

⮚ Null is often used to reset a variable or to indicate that an object is not yet available.

⮚ The type of null is object.

let data = null;            // data is explicitly set to no value
console.log(data);         // Output: null
console.log(typeof data); // Output: object

/*Output
null,
object
*/

5. Undefined

The Undefined data type represents a variable that has been declared but not yet assigned a value. It is the default value for uninitialized variables. If a function does not explicitly return a value, it implicitly returns undefined.

let x;
console.log(x);         // Output: undefined
console.log(typeof x); // Output: undefined

/*Output
undefined,
undefined
*/

6. Symbol

A Symbol is a special type of data in JavaScript (and Node.js) used to create unique identifiers. Unlike other data types, each Symbol is guaranteed to be unique, even if two Symbols have the same description.

⮚ Each Symbol is unique. Even if two Symbols are created with the same description, they are different.

let x=Symbol('Hello')
let y=Symbol('Hello')

console.log(x)      //output: Symbol(Hello)
console.log(y)     // output: Symbol(Hello)
console.log(x==y) //output: False

/*Output
    Symbol(Hello)
    Symbol(Hello)
    false    
*/

7. Array

An array is a special data type in JavaScript (and Node.js) used to store multiple values in a single variable. Arrays are ordered collections of data, which can be of any data type, including numbers, strings, objects, and even other arrays. They are commonly used to store lists of items and provide various methods to manipulate the stored data.

⮚ You can create an array using square brackets [] or the Array constructor.

let fruits1 = ["apple", "banana", "cherry"];
let fruits2 = new Array("apple", "banana", "cherry");

console.log(fruits1);
console.log(fruits2);

/*OUTPUT
    [ 'apple', 'banana', 'cherry' ]
    [ 'apple', 'banana', 'cherry' ]
*/

8. Object

An object is a special data type in JavaScript (and Node.js) used to store collections of data and more complex entities. Objects are key-value pairs, where each key (also called a property) has a value. The value can be any data type, including another object.

⮚ You can create an object using curly braces '{}' or the 'Object' constructor.

let person1 = {
    name: "Arjun",
    age: 30,
    isStudent: true
};

let person2 = new Object({
    name: "Karan",
    age: 26,
    isStudent: false
});

console.log(person1);
console.log(person2);

/*OUTPUT
    { name: 'Arjun', age: 30, isStudent: true }
    { name: 'Karan', age: 26, isStudent: false }
*/