DEV Community

Mehul Lakhanpal
Mehul Lakhanpal

Posted on • Originally published at codedrops.tech

Add properties to functions

Functions in JS are kind of an object (callable object). Hence, they can also store properties.

function func() {
    console.log('Hello World');
};

func.message = 'abc';

console.log(func()); // Hello World
console.log(func.message); // abc
Enter fullscreen mode Exit fullscreen mode

Thanks for reading πŸ’™

Follow @codedrops.tech for daily posts.

Instagram ● Twitter ● Facebook

Micro-Learning ● Web Development ● Javascript ● MERN stack ● Javascript

codedrops.tech

Top comments (2)

Collapse
 
amt8u profile image
amt8u

That's a very short article:-D. Just to add, functions are first class objects so everything that is applicable to objects is also true for functions. The problem is how dev tools outputs differently for functions and objects. If you do console.log(func), you will see the function definition which leads us to believe that it cannot have properties. To see the function properties as object you can use console.dir(func).

Collapse
 
ml318097 profile image
Mehul Lakhanpal

Yes, I am focusing on short articles only. Anyways thanks for the knowledge 😊