Using JavaScript’s built-in objects

By Akash
on 27-10-2023 12:08 PM

Built-in JavaScript objects and functions are ones you’ll use all the time. Here’s how to use Object, JSON, String, Math, Date, and console in your JavaScript programs.

JavaScript’s built-in objects include Object, JSON, console, String, Math, Date, and the window and global objects. These are some of the most important and useful parts of the JavaScript language. Together, they are essential elements of the programming environment in both the browser and server-side platforms like Node.

Object is the root object of all prototypes in JavaScript. Aside from providing the foundation for the JavaScript object model, Object imparts important methods such as toString() and assign(). Every object in JavaScript has these methods thanks to Object. The toString() method is very simple. You can call it on anything, and if nothing has been defined, it’ll use the default version. Many objects, like arrays, do define their own version. Custom objects can also define a version. When you interpolate an object into a string literal—console.log(“This is my object: “ + myObject)—it will call the toString() method.
The assign() method is a mechanism for cloning objects. It is a static method on Object itself:
Note that assign() makes a shallow copy, meaning it doesn’t copy nested properties. Also, the copy has references to the same pointer properties (that is, object1.objectRef === object2.objectRef).
Of all of JavaScript’s built-in objects, JSON may be the most commonly used. It lets you transform between string JSON and live JSON objects. (For more about JavaScript Object Notation, see InfoWorld’s primer, What is JSON?.)