JavaScript Data Types
What are data types in JavaScript?
Data types in JavaScript define the type of data that can be stored and manipulated within a program. JavaScript has both primitive and reference data types.
What are the primitive data types in JavaScript?
Primitive data types in JavaScript include:
- Number: Represents both integer and floating-point numbers.
- String: Represents a sequence of characters enclosed in single, double, or backticks.
- Boolean: Represents a logical entity and can be either
trueorfalse. - Undefined: A variable that has been declared but has not been assigned a value is of type
undefined. - Null: Represents the intentional absence of any object value, indicating no value.
- Symbol: A unique and immutable primitive value, often used as object property keys.
- BigInt: A numeric type that can represent integers with arbitrary precision.
How do you check the data type of a variable in JavaScript?
You can check the data type of a variable using the typeof operator.
console.log(typeof 'Hello'); // string
console.log(typeof 123); // number
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
console.log(typeof null); // object (this is a known quirk in JavaScript)
console.log(typeof Symbol()); // symbol
console.log(typeof 123n); // bigint
What is the difference between == and === in terms of data types?
The == operator checks for value equality with type coercion, while the === operator checks for both value and type equality without type coercion. It's recommended to use === for strict equality checks.
console.log(0 == '0'); // true
console.log(0 === '0'); // false
What is an object in JavaScript?
An object is a collection of properties, where each property is defined as a key-value pair. Objects can hold multiple values and complex data types.
const person = {
name: 'Alice',
age: 30,
greet: function() {
console.log(`Hello, my name is ${this.name}`);
}
};
person.greet(); // Hello, my name is Alice
What is an array in JavaScript?
An array is a special type of object that represents a collection of ordered values. Arrays can hold multiple values of any type, including other arrays and objects.
const fruits = ['apple', 'banana', 'orange'];
console.log(fruits[1]); // banana
What is type coercion in JavaScript?
Type coercion is the automatic or implicit conversion of values from one data type to another. JavaScript performs type coercion during operations where different types are involved.
console.log('5' + 5); // '55' (string concatenation)
console.log('5' - 2); // 3 (string is converted to number)
How do you convert a string to a number in JavaScript?
You can convert a string to a number using the Number() function, the parseInt() function, or the parseFloat() function.
const str = '123.45';
const num1 = Number(str); // 123.45
const num2 = parseInt(str); // 123
const num3 = parseFloat(str); // 123.45
How do you convert a number to a string in JavaScript?
You can convert a number to a string using the String() function or by calling the toString() method on the number.
const num = 123;
const str1 = String(num); // '123'
const str2 = num.toString(); // '123'
What is the significance of undefined and null?
undefined indicates that a variable has been declared but has not yet been assigned a value, while null is an intentional assignment of a variable to indicate "no value" or "empty." They are often used to differentiate between the absence of a value and the absence of an assignment.
What are the different ways to create objects in JavaScript?
There are several ways to create objects in JavaScript:
- Object Literal:
const obj1 = { name: 'Alice', age: 30 };
- Using the
newKeyword:
const obj2 = new Object();
obj2.name = 'Bob';
obj2.age = 25;
- Using a Constructor Function:
function Person(name, age) {
this.name = name;
this.age = age;
}
const person1 = new Person('Charlie', 28);
- Using Object.create():
const proto = { greet: function() { console.log(`Hello, ${this.name}`); } };
const obj3 = Object.create(proto);
obj3.name = 'Diana';
What is a function object in JavaScript?
A function in JavaScript is also an object. It can be assigned to a variable, passed as an argument, and returned from another function. Functions can have properties and methods like any other object.
function myFunction() {
return 'Hello!';
}
myFunction.customProperty = 'This is a function property';
console.log(myFunction.customProperty); // This is a function property