Functions are Objects in JavaScript
I'm a college dropout turned self-taught developer. I transform documented ideas into functioning scalable tech products.
Search for a command to run...
I'm a college dropout turned self-taught developer. I transform documented ideas into functioning scalable tech products.
No comments yet. Be the first to comment.
Is JavaScript compiled language or interpreted language or a mix of both? Don't worry! Take a chill pill 😎 I'll break it down for you, bit by bit. But first thing first... Take a deep breath, and follow along. JavaScript engines use the JIT(just in ...
Have you ever been in a situation where your code runs in one browser and fails in another? And you would be wondering what the hell is happening?? Even after checking your code thoroughly, over and over again, you find it 100% correct. Then where t...
It took me 4-days and 100+ attempts to solve this simple issue of uploading a file using node. And the moment I got it right, I was like...phewww Here is how I solved this issue. By creating a - Simple node server. Middleware to process files. PO...
Again a jargonish word, but let me demystify it for you. It's nothing but a simple technique we use to prevent unnecessary function calls to improve app's performance. Unlike Debouncing, here we block a function call for a particular time if it's be...
Whattttt???

Yes, You read that correctly.
Let's deep dive into this topic.
Before we get into a serious drill, Let's quickly revise objects and functions in JavaScript.
Object:
Objects in JavaScript are nothing but a non-primitive type data structure. We can define an object using curly brackets and put key-value pairs separated by a comma in it.
const user = {
name:"Hemendra",
nickname:"Hemu",
email:"hi@aamchora.space",
}
user.city = "Bhilwara" // add new value in existing object
user.city = "Jaipur" // update new value in existing object
console.log(user.name);
/*
output
Hemendra
*/
console.log(user);
/*
output
{
name:"Hemendra",
nickname:"Hemu",
email:"hi@aamchora.space",
city:"Jaipur"
}
*/
Read more about objects in depth.
Function
Unlike other programming languages, JavaScript has implemented the functions very differently.
Let's have a look
Definition:
A function is a block of organized code that performs a specific task.
Advantages of functions
Now you would be wondering, What's the difference then?

In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. Every JavaScript function is actually a Function object.

Let's see how we define functions in JavaScript first
function sum(a,b){
return a+b;
}
const res = sum(2,4);
console.log(res); // outputs -> 6
Above we have created a function named sum which adds two numbers.
But as I said earlier, is the sum function created above also an object? can we add properties and values to the sum function as we did in the user object above?
let's see this in action
function sum(a,b){
return a+b;
}
sum.help = "use this function to add two numbers";
// like an object we have added help key and it's value to the above function.
// will work as before
const res = sum(3,6);
console.log(res) // outputs --> 9
// but now
console.log(sum.help)
// outputs --> use this function to add two numbers
Let's verify if the sum function is an instance of an object or not
function sum(a,b){
return a+b;
}
console.log(sum instanceof Function); // true
console.log(sum instanceof Object); // true
let name = "Hemendra"
console.log(name instanceof Object); // false
And Voila, It proves that the functions are objects in JavaScript.

follow me for more such blog posts.
Let me know if this blog was helpful.