Search code examples
javascriptobject

Why "function" keyword is necessary before object name ?All objects are function in real?


When we make an object like this

function myObject(){  
Properties and methods here----
}; 

We write “function” keyword before name of object is it necessary? All objects are functions in real? Can we not write direct object name like this?

myObject(){  
Properties and methods here----
}; 

Solution

  • No, not all objects are functions. (All functions are objects, though.)

    Here, obj isn't a function:

    var obj = {
        foo: "bar"
    };
    

    Nor dt here:

    var dt = new Date();
    

    The function keyword is necessary in order to say "what follows is a function declaration or function expression." It's just part of the basic syntax of JavaScript.