Skip to main content

Command Palette

Search for a command to run...

Function Bulding blocks of javascript

Published
1 min readView as Markdown

Function declaration and function expressions

Function declaration is are one the most straightforward ways to define a function in js they are using a function keyword followed by a name,parameters and a function body.

Hoisting: Function declarations are hoisted to the top of their enclosing scope. This means the entire function is moved to the top of its scope, making it available throughout the code before it's defined. This behavior can be helpful but may also lead to unexpected results if not well understood.

Function Expression: A function Expression involves defning a function as expression . this can be done either anonymously or with a name

Key Characteristics of Function Expressions:

1 Not Hoisted : it can be not hoisted like a function expressions .

2 Anonymous Functions: Function expressions can be anonymous, meaning they do not have to have a name. This can be useful for creating one-off functions or passing functions as arguments to other functions.

Arrow function: it can we a introduced in ES6 provide more concise way to writing a function expressions.

ex const greet=(name)=>`Hello ,${name}`;

Lexical this Binding: Arrow functions do not have their own this context. Instead, they inherit this from the surrounding scope. This can be very useful in scenarios where you want to preserve the value of this.

More from this blog

article

10 posts