How many ways of creating an Object in JavaScript!

How many ways of creating an Object in JavaScript

Using the Object() constructor:

var d =newObject();

This is the simplest way to create an empty object. I believe it is now discouraged.

Using Object.create() method:

var a =Object.create(null);

This method creates a new object extending the prototype object passed as a parameter.

Using the bracket’s syntactig sugar:

var b ={};

This is equivalent to Object.create(null) method, using a null prototype as an argument.

Using a function constructor

varObj=function(name){ = name
}var c =newObj("hello");

What the new operator does is call a function and setting this of the function to a fresh new Object, and binding the prototype of that new Object to the function’s prototype. As is:

function f {};new f(a, b, c);Would be equivalent to:// Create a new instance using f's prototype.var newInstance =Object.create(f.prototype)var result;// Call the function
result =, a, b, c),// If the result is a non-null object, use it, otherwise use the new instance.
result &&typeof result ==='object'? result : newInstance

Using the function constructor + prototype:

function myObj(){}; ="hello";var k =new myObj();

Using ES6 class syntax:

class myObject  {constructor(name){ = name;}}var e =new myObject("hello");

Singleton pattern:

var l =newfunction(){ ="hello";}

Deleting node_modules folder

If you face problem in deleting your node_modules folder while creating application using npm, then you can use this easy way to get rid of the node_modules folder.

This is a short tutorial explaining to delete folder directly from command prompt. Make your life easy.

npm install rimraf -g

rimraf node_modules

After running this command you will notice that the folder gets deleted in a while. You need to wait little longer if you had more packages installed. So be patient.

FAQs which are asked in JavaScript interview

Question: What is the value of foo?

var foo = '1' + 2 + 3;

Answer: '123'

var foo = 1 + 2 + '3';

Answer: '33'


Question: What value is returned from the following statement?

"i'm a boy".split("").reverse().join("");

Answer: It’s actually a reverse method for a string – "yob a m'i"


Question: What is the value of

( || ( = "bar" ) );

Answer: Always 'bar'


Question: What does the following code print?

setTimeout(function() {
}, 0);

Answer: one, three and two. It’s because console.log('two'); will be invoked in the next event loop.


Question: What is the value of foo.x?

var foo = {n: 1};
var bar = foo;
foo.x = foo = {n: 2};

Answer: undefined. Rather, bar.x is {n: 2}.

foo.x = foo = {n: 2} is the same as foo.x = (foo = {n: 2}). It is because a left term is first referenced and then a right term is evaluated when an assignment is performed in JavaScript. When foo.x is referenced, it refers to an original object, {n: 1}. So, when the result of the right term, {n: 2}, is evaluated, it will assigned to the original object, which is at the moment referenced by bar.

Methods in class (JavaScript)

You will create three types of methods in the class –

  1. Constructor
  2. Static, and
  3. Prototype

1. Constructor method

A constructor method is a special method. Constructor method creates and initializes the object created with the class. A class can have only one constructor method. More than one constructor method in the class will throw an exception.

class Student {
    constructor(name, age) { = name;
        this.age = age;

    getData() {
        console.log( + " is " + this.age + " years old !");

var s1 = new Student("foo", 7);


2. Static methods

Like any other programming languages, JavaScript static methods are called without creating the object of the class.

JavaScript does not have static members/properties. As of now, it only supports static methods.

class Student {
    static LogStudent() {
        return "I am static method";

var n = Student.LogStudent();


3. Prototype methods

JavaScript class prototypes methods are the methods used by the instances of the class. They are the normal methods, which can be inherited and used by the objects.

class Student {
    constructor(name, age){ = name;
         this.age = age;

Student.prototype.getData = function () {
    return + " is " + this.age + " years old !";

var s1 = new Student("foo", 8);



I hope it helps. Thanks for reading.