Function Declaration vs Expressions

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 140:- Function Declaration vs Expressions

In JavaScript, there are two ways to create functions: function declarations and function expressions.

A function declaration is a statement that declares a named function with the specified parameters. The function is created at the time the code is parsed, which means that it can be called anywhere in the code, even before it is defined.

Example of function declaration:

cssCopy code

function add(a, b) {  return a + b; }

A function expression, on the other hand, is a function that is assigned to a variable. It is created at the time the code is executed, which means that it can only be called after it has been defined.

Example of function expression:

cssCopy code

const add = function(a, b) {  return a + b; }

One important difference between function declarations and expressions is that function declarations are hoisted to the top of the code, which means that they can be called before they are defined. Function expressions are not hoisted, so they can only be called after they are defined.

Another difference is that function declarations create a local variable in the current scope with the same name as the function, while function expressions do not.

In general, function declarations are preferred for their hoisting and ability to be called before they are defined, while function expressions are preferred for their flexibility and the ability to pass them as arguments to other functions.

16. JavaScript - Functions and Array

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

I am not able to access videos from second class and further. I have already completed first class

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support