Native Objects

Native objects are built-in objects in JavaScript. They are pre-defined and are always available to us as long as we are using JavaScipt irrespective of the environment the JavaScript code is running. Some commonly used Native Objects in JavaScript are:

// Using string native object var s = new String ("I am a string literal wrapped by an object wrapper"); console.log(typeof s); //object //converting string object to string literal s = s.toString(); console.log(typeof s); //string //an example of string literal var str = "I am a string literal"; console.log(typeof str); //string

new String("I am a string literal wrapped by an object wrapper")

string

"I am a string literal wrapped by an object wrapper"

string

number

boolean

number

boolean

new

Host Objects

Host objects are the objects which are created and provided to JavaScript by the environment (host) which hosts the code. Be it in Node environment or in browser, the JavaScript code will behave differently when executed in different JavaScript engines. Since JavaScript always works in the context of the hosting environment, the result of executed code may differ depending on the current host.





console

.log()

.error()

.table()

dir()

info()

profile()

console

console

bar(); var a = true; if (a) { function bar(){console.log("print a");} } else { function bar(){console.log("print b");} }

Uncaught TypeError: bar is not a function

print b

console

console

Have a look at the following example:In the above code snippet, the result of the constructor form of value creationis an object wrapper around theliteral*value.This is also true forandobjects becauseandobjects are also denoted bykeyword and can be used as native constructors unlike their primitive counterparts.One famous host object which developers interact regularly is -object. It has several methods likeetc.object is provided by hosting environment so that JavaScript code can interact with the host for development related job. It may perform differently depending on the hosting environment which supplies theobject.Consider the following code snippet:We get the following output if we run this above piece of code in :The above code has executed differently in Chrome and node.js because in Chrome,connects to the developer tools’ console display, whereas in node.js and other server-side JavaScript environments,as a global instance is configured to write to the standard-output (stdout) and standard-error (stderr) streams of the JavaScript environment.