JavaScript
java script
awasthi sir
8/25/20243 min read
1. What are the different data types in JavaScript?
Answer: JavaScript has several data types:
Primitive Types: undefined, null, boolean, number, string, symbol, bigint.
Non-Primitive Types: object (including arrays, functions, dates, etc.).
2. What is the difference between let, var, and const?
Answer:
var is function-scoped and can be redeclared and updated.
let is block-scoped and cannot be redeclared in the same scope but can be updated.
const is block-scoped, cannot be redeclared or updated.
1. Primitive Data Types
Primitive data types are the most basic data types in JavaScript. They are immutable, meaning their values cannot be changed once they are created. Each variable holding a primitive type directly contains the value, rather than a reference to an object.
The primitive data types in JavaScript include:
Number
Represents both integer and floating-point numbers.
Example: let age = 25;, let price = 19.99;
String
Represents a sequence of characters (text).
Example: let name = "Alice";
Boolean
Represents one of two values: true or false.
Example: let isStudent = true;
Undefined
Represents a variable that has been declared but not assigned a value.
Example: let x; (Here, x is undefined.)
Null
Represents the intentional absence of any object value.
Example: let y = null;
Symbol (introduced in ES6)
Represents a unique and immutable value that can be used as an identifier for object properties.
Example: let sym = Symbol("id");
BigInt (introduced in ES2020)
Represents integers with arbitrary precision. It’s used for working with large numbers that exceed the safe integer limit for the Number type.
Example: let bigNumber = 123456789012345678901234567890n;
2. Non-Primitive (Reference) Data Types
Non-primitive data types are complex data types. Variables of non-primitive types do not directly contain the value; instead, they hold a reference to the value stored in memory. These types are mutable, meaning the values can be changed.
The non-primitive data types in JavaScript include:
Object
An object is a collection of properties, where each property is a key-value pair.
Array
An array is a list-like object used to store multiple values in a single variable.
Example: let numbers = [1, 2, 3, 4, 5];
Date
Represents date and time.
Example: let today = new Date();
RegExp
Represents a regular expression, used for pattern matching within strings.
Example: let pattern = /abc/;
Map
A Map holds key-value pairs, and the keys can be of any data type.
Set
A Set is a collection of unique values, meaning no duplicate values are allowed.
In JavaScript, data types are categorized into two main types: primitive and non-primitive (also known as reference types).
1. Primitive Data Types
Primitive data types are the most basic data types in JavaScript. They are immutable, meaning their values cannot be changed once they are created. Each variable holding a primitive type directly contains the value, rather than a reference to an object.
The primitive data types in JavaScript include:
Number
Represents both integer and floating-point numbers.
Example: let age = 25;, let price = 19.99;
String
Represents a sequence of characters (text).
Example: let name = "Alice";
Boolean
Represents one of two values: true or false.
Example: let isStudent = true;
Undefined
Represents a variable that has been declared but not assigned a value.
Example: let x; (Here, x is undefined.)
Null
Represents the intentional absence of any object value.
Example: let y = null;
Symbol (introduced in ES6)
Represents a unique and immutable value that can be used as an identifier for object properties.
Example: let sym = Symbol("id");
BigInt (introduced in ES2020)
Represents integers with arbitrary precision. It’s used for working with large numbers that exceed the safe integer limit for the Number type.
Example: let bigNumber = 123456789012345678901234567890n;
2. Non-Primitive (Reference) Data Types
Non-primitive data types are complex data types. Variables of non-primitive types do not directly contain the value; instead, they hold a reference to the value stored in memory. These types are mutable, meaning the values can be changed.
The non-primitive data types in JavaScript include:
Object
An object is a collection of properties, where each property is a key-value pair.
Example:
javascript
Copy code
let person = { name: "Alice", age: 25, isStudent: true };
Array
An array is a list-like object used to store multiple values in a single variable.
Example: let numbers = [1, 2, 3, 4, 5];
Function
A function is a block of code designed to perform a particular task.
Example:
javascript
Copy code
function greet() { return "Hello, World!"; }
Date
Represents date and time.
Example: let today = new Date();
RegExp
Represents a regular expression, used for pattern matching within strings.
Example: let pattern = /abc/;
Map
A Map holds key-value pairs, and the keys can be of any data type.
Example:
javascript
Copy code
let map = new Map(); map.set('key1', 'value1');
Set
A Set is a collection of unique values, meaning no duplicate values are allowed.
Example:
javascript
Copy code
let set = new Set([1, 2, 3]);
Key Differences Between Primitive and Non-Primitive Types
Immutability: Primitive types are immutable, whereas non-primitive types are mutable.
Memory Storage: Primitive types are stored directly in the location that the variable accesses, whereas non-primitive types store a reference to the object in memory.
Comparisons: Primitive values are compared by value, while non-primitive values (objects) are compared by reference.