Functions are Objects in javaScript
Tags: javascript, webdev, programming, tutorial
Published: 2022-07-05 10:00:00

---
canonical_url: https://blog.aamchora.space/functions-are-objects-in-javascript
---

**Whattttt???**

![Confused man](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q610q7szfdlkbgvrqqen.gif)

 
**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.

```js
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.](https://dev.to/aamchora/javascript-objects-in-depth-5g71)


**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**

- Functions make your program easier to read, understand, and debug.
- Functions can make a program smaller by eliminating repetitive code.


Now you would be wondering, What's the **difference** then? 

![What is different](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qnlhgt64dggwj4ab580p.gif)
 

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**.


![Now way gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rb7zqh9hx4xv376yeqn0.gif)
 

Let's see how we define functions in JavaScript first

```js
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

```js

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.

```

```js
// 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

```js

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.

![I was right gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vasb78g9cnezilh95nxg.gif)