What are JavaScript Native Objects?


In this tutorial, we will learn about native objects in JavaScript.

Native JavaScript objects are regular JavaScript objects offered by JavaScript itself. Inbuilt objects, pre-defined objects, and global objects are other names. No matter the computer or environment, all users have access to these objects, and they function similarly. They may be used as both constructors (like String(), Array(), and Object()) and primitive values since their functions are unaffected by changes to the machine or environment.

An object in an ECMAScript implementation whose semantics are entirely determined by this specification as opposed to the host environment. Native objects are additionally known as Built-in Objects.

There are different kinds of native objects specified in JavaScript.

JavaScript Number Object

Integers, decimal, or float point numbers, among many other types of numbers, are all represented as number objects. The Number object is a fundamental wrapper object that represents and manages numbers. Values of various kinds can be turned into numbers using the Number() method. Primitive values like integers often don't have any related methods. The Number() method returns the number format for each type of JavaScript variable. It returns Nan if the supplied value cannot be transformed into a number. Although JavaScript treats primitive values as objects, Nan stands for "Not a number."

Syntax

var num = new Number(Val);

The variable "num" stores the number value of the "val" argument provided to the Number object.

JavaScript Boolean Object

The JavaScript Boolean object is a wrapper class and a member of global objects. Depending on the value supplied while generating the Boolean object, it is used to produce a Boolean object that either has a true or false value. When values like 0, -0, an empty text (""), false, null, or Not a Number (NaN) are provided to the Boolean object when it is being created, the Boolean object returns false. All other values, including an empty array([]), an empty object(), or the text "false," will set the initial value for the Boolean object to true in contrast to all these values, which set the initial value as false for the Boolean object.

Syntax

let bool = new Boolean(val);

The variable bool stores the Boolean value of the "val" parameter, which is converted by the Boolean object.

JavaScript String Object

The JavaScript string basic data type is wrapped in the String object, which provides a variety of assistance methods for working with strings of characters. You may call any of the helper methods of the String object on a string primitive since JavaScript automatically translates between string primitives and String objects.

Syntax

var val = new String(string);

The "val" variable stores the string value, converted by the string native object in JavaScript.

JavaScript Date Object

The JavaScript language includes the Date object as a datatype built-in. The new Date() constructor creates Date objects. Using various methods, you may perform operations on a Date object after it has been created. Most methods enable you to access and modify an object's year, month, day, hour, minute, second, and millisecond properties in either local or UTC (also known as GMT) time.

Syntax

var val = new Date();

The "val" variable stores the current date using the date object.

JavaScript Array Object

You may store several values in a single variable using the Array object. A fixed-size sequential collection of identical-type pieces is kept in it. It is important to conceive of an array as a collection of variables of the same type, even if it is used to hold data collection.

Syntax

var fruits = new Array( "strawberry", "grape", "peach" );

The array is taken as input and stored in the fruits variable.

JavaScript Math Object

Using the math object, you may access characteristics and techniques for mathematical constants and functions. Math is not a constructor, in contrast to other global objects. Math may be used as an object without being created, and all of its attributes and methods are static and available for use.

Syntax

var pi1 = Math.PI;

The math object stores the value of pi in the pi1 variable.

JavaScript RegExp Object

A character pattern can be described using a regular expression. The JavaScript RegExp class represents regular expressions. And also both String and RegExp offer methods that apply regular expressions to text to perform robust pattern matching and search and replace operations.

Syntax

let pattern = /Hi user/i;

The regular expression is specified in this syntax. It is a string containing the regular expression's pattern.

Example

In this example, all the native objects in JavaScript are described. The Boolean, array, date, string, number, math, and regular expression object are specified in this example. The objects are created, and a value is provided for each object to display its creation.

<html> <body> <h2> JavaScript Native Objects <i> compilation </i> </h2> <p id="demo"> </p> </body> <script> document.getElementById("demo").innerHTML = "Boolean Object: " + new Boolean(true) + "<br>"; document.getElementById("demo").innerHTML += "Array Object: " + new Array([5, 6, 7, 18]) + "<br>"; document.getElementById("demo").innerHTML += "Date Object: " + new Date() + "<br>"; document.getElementById("demo").innerHTML += "String Object: " + new String("Hello World Starts") + "<br>"; document.getElementById("demo").innerHTML += "Number Object: " + new Number(777) + "<br>"; document.getElementById("demo").innerHTML += "Math Object: " + Math.PI + "<br>"; let text = "HI User"; let pattern = /Hi user/i; let result = text.match(pattern); document.getElementById("demo").innerHTML += "RegExp Object: " + result + "<br>"; </script> </html>

In this tutorial, we learned about different kinds of native objects specified in JavaScript, like String, Boolean, RegExp, Math, Date, Number, and Array.

Updated on: 15-Nov-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements