Back to Course

Introduction to JavaScript

0% Complete
0/0 Steps
  1. JavaScript: Basics
    9Topics
    |
    10 Quizzes
  2. JavaScript: Variables
    9Topics
    |
    10 Quizzes
  3. JavaScript: Conditional Statements
    10Topics
    |
    11 Quizzes
  4. JavaScript: Functions
    10Topics
    |
    11 Quizzes
  5. JavaScript: Scope
    6Topics
    |
    7 Quizzes
  6. JavaScript: Arrays
    11Topics
    |
    12 Quizzes
  7. JavaScript: Loops
    8Topics
    |
    9 Quizzes
  8. JavaScript: High Order Functions
    3Topics
    |
    4 Quizzes
  9. JavaScript: Iterators
    6Topics
    |
    6 Quizzes
Lesson 4, Topic 8
In Progress

JavaScript: Function Expressions

Yasin Cakal 7 Nov 2021
Lesson Progress
0% Complete

A function expression is another way to define a function. The function keyword can be used to define a function within an expression. The function name is usually omitted from a function expression. An anonymous function is a function that has no name. In order to refer to a function expression, it is frequently stored in a variable.

Consider the following example:

const weekend = function(day) {
  if(day === 'Saturday'|| day === 'Sunday'){
    return true;
  } else {
    return false;
  }
};

To declare a function expression, use the following syntax:

  • Since the release of ES6, it has become common practice to declare variables with the keyword const. Declare a variable with the name of your function as the variable’s name (or identifier).
  • Assign an anonymous function created by using the function keyword followed by a set of parentheses ( ) with possible parameters as that variable’s value. The function body is then contained by a set of curly braces { }.

To call a function expression, type the name of the variable where the function is stored, followed by parentheses( ) enclosing any arguments. Here is an example of calling function expression:

const weekend = function(day) {
  if(day === 'Saturday'|| day === 'Sunday'){
    return true;
  } else {
    return false;
  }
};
console.log(weekend('Tuesday'));

//Output: false

Function expressions, unlike function declarations, are not hoisted, so they can’t be called before they’ve been defined.

Responses

Leave a Reply

%d bloggers like this: