3. Data Types and Objects in JavaScript
Primary data types
→ They are immutable
→ JS have seven primitive data types
String | Strings are sequences of characters enclosed in single (' ), double (" ) or backticks (` ) for template literals. |
number | Used for integers and floating-point numbers. |
bigint | Represents large integers |
boolean | Represents true or false values. |
undefined | JavaScript automatically assigns undefined to uninitialized variables. |
null | Represents an intentional absence of value. (we initilize some varibale as null manually) It is not assigned automatically like undefined |
symbol | Used to create unique identifiers. |
Non Primitive data types ( objects , stores by refrence)
Objects are the collection of key- value pairs and value can be of any type , including function and other object.
We create object using following ways in JS:
- Using Object Literals
let person = {
name: "John",
age: 30,
greet: function() { // value is of function type here
console.log("Hello, " + this.name);
}
};
person.greet(); // Hello, John
2. Using new Object()
let car = new Object();
car.brand = "Toyota";
car.model = "Corolla";
car.getInfo = function() {
return `${this.brand} ${this.model}`;
};
console.log(car.getInfo()); // Toyota Corolla
Built-in Objects in JS
Built-in objects, or “global objects”, are the objects that are accessible at the global scope.
Some Imp. built in objects are
1. Math Object - Provides mathematical functions and constants.
2. Date Object - Handles date and time.
3. String Object - Provides string manipulation methods.
4. Array Object - Handles lists of data.
5. JSON Object - Used for working with JSON data.
// Maths Object
console.log(Math.PI); // 3.141592653589793
console.log(Math.sqrt(16)); // 4
console.log(Math.random()); // Random number between 0 and 1
// date object
let now = new Date();
console.log(now.toDateString()); // Example: Mon Feb 06 2025
// String object
let text = "Hello, World!";
console.log(text.length); // 13
console.log(text.toUpperCase()); // HELLO, WORLD!
console.log(text.includes("World")); // true
// Arary object
let numbers = [10, 20, 30];
console.log(numbers.push(40)); // Adds 40 to the array
console.log(numbers.pop()); // Removes last element
//JSON Object
let jsonString = '{"name": "John", "age": 30}';
let obj = JSON.parse(jsonString);
console.log(obj.name); // John
console.log(JSON.stringify(obj)); // Convert back to JSON
typeof
operatorThe
typeof
operator is used to determine the type of a variable.
console.log(typeof 42); // "number"
console.log(typeof "Hello"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof {}); // "object"
console.log(typeof []); // "object" (Arrays are objects)
console.log(typeof function(){}); // "function"
console.log(typeof null); // "object" (This is a JavaScript bug)
console.log(typeof undefined); // "undefined"
Interview Questions
What is the difference between
null
andundefined
?undefined
means a variable is declared but not assigned a value.null
is explicitly assigned to indicate "no value".What will be the output of the following?
console.log(typeof NaN); console.log(typeof null);
number
object
How do you add and delete properties from an object?
let person = { name: "Alice" }; person.age = 25; // Adding property delete person.name; // Deleting property console.log(person); // { age: 25 }
What is the difference between
JSON.parse()
andJSON.stringify()
?JSON.parse()
converts a JSON string into an object.JSON.stringify()
converts an object into a JSON string.
let jsonStr = '{"name": "John"}';
let obj = JSON.parse(jsonStr); // Converts to object
console.log(obj.name); // John
let str = JSON.stringify(obj); // Converts to JSON string
console.log(str); // '{"name":"John"}'
How can you generate a random number in JavaScript?
let randomNum = Math.random(); // Between 0 and 1 let randomInt = Math.floor(Math.random() * 10) + 1; // Between 1 and 10
Intermediate Topics
Object Prototype
Prototypal Inheritence