JavaScript Objects
A JavaScript object is a collection of properties, where each property is defined as a key-value pair. Objects are fundamental to JavaScript and are used to store and manage data. Here’s an overview and examples of JavaScript objects:
Defining an Object
1. Using Object Literals
const person = {
name: ‘John’,
age: 30,
greet: function() {
console.log(‘Hello, my name is ‘ + this.name);
}
};
2. Using the new Object() Syntax
const person = new Object();
person.name = ‘John’;
person.age = 30;
person.greet = function() {
console.log(‘Hello, my name is ‘ + this.name);
};
Accessing and Modifying Properties
1. Dot Notation
console.log(person.name); // Output: John
person.age = 35;
console.log(person.age); // Output: 35
2. Bracket Notation
console.log(person[‘name’]); // Output: John
person[‘age’] = 35;
console.log(person[‘age’]); // Output: 35
Adding and Deleting Properties
1. Adding Properties
person.address = ‘123 Main St’;
console.log(person.address); // Output: 123 Main St
2. Deleting Properties
delete person.age;
console.log(person.age); // Output: undefined
Nested Objects
Objects can contain other objects, allowing for complex data structures.
const person = {
name: ‘John’,
age: 30,
address: {
street: ‘123 Main St’,
city: ‘New York’,
country: ‘USA’
},
greet: function() {
console.log(‘Hello, my name is ‘ + this.name);
}
};
console.log(person.address.city); // Output: New York
Methods
Objects can also have methods, which are functions defined as properties.
const calculator = {
add: function(a, b) {
return a + b;
},
subtract: function(a, b) {
return a – b;
}
};
console.log(calculator.add(5, 3)); // Output: 8
console.log(calculator.subtract(5, 3)); // Output: 2
Iterating Over Properties
You can iterate over the properties of an object using a for…in loop.
const person = {
name: ‘John’,
age: 30,
address: ‘123 Main St’
};
for (let key in person) {
console.log(key + ‘: ‘ + person[key]);
}
// Output:
name: John
age: 30
address: 123 Main St
Built-in Object Methods
1. Object.keys(): Returns an array of the object’s own property names.
console.log(Object.keys(person)); // Output: [‘name’, ‘age’, ‘address’]
2. Object.values(): Returns an array of the object’s own property values.
console.log(Object.values(person)); // Output: [‘John’, 30, ‘123 Main St’]
3. Object.entries(): Returns an array of the object’s own key-value pairs.
console.log(Object.entries(person));
// Output: [[‘name’, ‘John’], [‘age’, 30], [‘address’, ‘123 Main St’]]
Math Object
The Math object in JavaScript is a built-in object that has properties and methods for mathematical constants and functions. It’s not a function object, so it doesn’t have a constructor. Here’s an overview of the Math object and examples of its commonly used properties and methods:
Math Object Properties
1. Math.PI: The ratio of the circumference of a circle to its diameter, approximately 3.14159.
console.log(Math.PI); // Output: 3.141592653589793
2. Math.E: Euler’s constant, the base of natural logarithms, approximately 2.718.
console.log(Math.E); // Output: 2.718281828459045
3. Math.LN2: The natural logarithm of 2, approximately 0.693.
console.log(Math.LN2); // Output: 0.6931471805599453
4. Math.LN10: The natural logarithm of 10, approximately 2.302.
console.log(Math.LN10); // Output: 2.302585092994046
5. Math.SQRT2: The square root of 2, approximately 1.414.
console.log(Math.SQRT2); // Output: 1.4142135623730951
Math Object Methods
1. Math.abs(x): Returns the absolute value of a number.
console.log(Math.abs(-5)); // Output: 5
2. Math.ceil(x): Rounds a number up to the nearest integer.
console.log(Math.ceil(4.3)); // Output: 5
3. Math.floor(x): Rounds a number down to the nearest integer.
console.log(Math.floor(4.7)); // Output: 4
4. Math.round(x): Rounds a number to the nearest integer.
console.log(Math.round(4.5)); // Output: 5
console.log(Math.round(4.4)); // Output: 4
5. Math.max(…values): Returns the largest of zero or more numbers.
console.log(Math.max(10, 20, 30)); // Output: 30
6. Math.min(…values): Returns the smallest of zero or more numbers.
console.log(Math.min(10, 20, 30)); // Output: 10
7. Math.pow(base, exponent): Returns the base to the exponent power.
console.log(Math.pow(2, 3)); // Output: 8
8. Math.sqrt(x): Returns the square root of a number.
console.log(Math.sqrt(16)); // Output: 4
9. Math.random(): Returns a pseudo-random number between 0 (inclusive) and 1 (exclusive).
console.log(Math.random()); // Output: a random number between 0 and 1
10. Math.trunc(x): Returns the integer part of a number by removing any fractional digits.
console.log(Math.trunc(4.9)); // Output: 4
11. Math.sign(x): Returns the sign of a number, indicating whether the number is positive, negative, or zero.
console.log(Math.sign(3)); // Output: 1
console.log(Math.sign(-3)); // Output: -1
console.log(Math.sign(0)); // Output: 0
12. Math.cbrt(x): Returns the cube root of a number.
console.log(Math.cbrt(27)); // Output: 3
13. Math.log(x): Returns the natural logarithm (base e) of a number.
console.log(Math.log(1)); // Output: 0
14. Math.exp(x): Returns e^x, where x is the argument and e is Euler’s constant.
console.log(Math.exp(1)); // Output: 2.718281828459045
15. Math.hypot(…values): Returns the square root of the sum of squares of its arguments.
console.log(Math.hypot(3, 4)); // Output: 5