JavaScript is one of the programming languages widely used and popular in today's world. One of the powerful features of this language is the use of objects and the ability to work with them easily. Built-in objects related to objects in JavaScript are tools that allow programmers to easily manage the behaviors and data present in an object.
As a programmer, it is important to recognize different built-in objects that can act on objects and utilize them. These objects are usually extensive and flexible, using them can help perform many complex tasks simply.
In this article, we will explore various common and important built-in objects in JavaScript. By using these objects, you can manage the data present in an object, update, and remove it efficiently.
Key built-in objects such as Object.keys()
, Object.values()
, and Object.entries()
can help you easily access keys, values, and key-value pairs of an object. We will discuss more about these objects in detail.
Code Example for Built-in Objects in JavaScript
const person = {
name: 'Ali',
age: 30,
city: 'Tehran'
};
// Using Object.keys()
const keys = Object.keys(person);
console.log(keys); // ['name', 'age', 'city']
// Using Object.values()
const values = Object.values(person);
console.log(values); // ['Ali', 30, 'Tehran']
// Using Object.entries()
const entries = Object.entries(person);
console.log(entries); // [['name', 'Ali'], ['age', 30], ['city', 'Tehran']]
Line-by-Line Explanation of the Code
const person = { name: 'Ali', age: 30, city: 'Tehran' };
An object named
person
is defined, which contains some information like name, age, and city. const keys = Object.keys(person);
The method
Object.keys()
retrieves all the keys of the object person
and stores them in an array. console.log(keys);
The array of keys will be printed to the console. In this case, the result will include
['name', 'age', 'city']
. const values = Object.values(person);
The method
Object.values()
retrieves all the values of the object person
and stores them in an array. console.log(values);
The array of values will be printed to the console. In this case, the result will include
['Ali', 30, 'Tehran']
. const entries = Object.entries(person);
The method
Object.entries()
retrieves all the key-value pairs of the object person
and stores them in an array of arrays. console.log(entries);
The array of key-value pairs will be printed to the console. In this case, the result will include
[['name', 'Ali'], ['age', 30], ['city', 'Tehran']]
.