101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    JavaScript 101

    Receive aemail containing the next unit.
    • Introduction to JavaScript
      • 1.1What is JavaScript?
      • 1.2Setting up your environment
      • 1.3Basic syntax and variables
    • Data Types and Variables
      • 2.1Understanding data types
      • 2.2Variables and constants
      • 2.3Operators
    • Control Structures
      • 3.1Conditional statements
      • 3.2Loops
      • 3.3Error handling
    • Functions
      • 4.1Defining functions
      • 4.2Function expressions
      • 4.3Arrow functions
    • Objects and Arrays
      • 5.1Understanding objects
      • 5.2Understanding arrays
      • 5.3Array methods
    • Document Object Model (DOM)
      • 6.1What is the DOM?
      • 6.2Selecting and manipulating elements
      • 6.3Event handling
    • Asynchronous JavaScript
      • 7.1Callbacks
      • 7.2Promises
      • 7.3Async/Await
    • API Interaction
      • 8.1What is an API?
      • 8.2Fetch API
      • 8.3Working with JSON
    • Debugging
      • 9.1Console methods
      • 9.2Debugging tools
      • 9.3Common JavaScript errors
    • JavaScript Libraries
      • 10.1Introduction to libraries
      • 10.2Using jQuery
      • 10.3Using lodash
    • JavaScript Frameworks
      • 11.1Introduction to frameworks
      • 11.2Using React
      • 11.3Using Vue.js
    • Testing and Deployment
      • 12.1Writing tests with Jest
      • 12.2Deployment with Netlify
      • 12.3Continuous integration
    • Final Project
      • 13.1Project proposal
      • 13.2Project development
      • 13.3Project presentation

    Functions

    Understanding Function Expressions in JavaScript

    high-level programming language

    High-level programming language.

    In JavaScript, functions are first-class objects. This means that, like other objects, you can pass functions as arguments to other functions, return them as the values from other functions, and assign them to variables or store them in data structures. A function expression is a way to define a function as an expression, rather than as a function declaration.

    What is a Function Expression?

    A function expression in JavaScript is a function that is assigned to a variable. The function can be named, or it can be anonymous (i.e., it does not have a name). The name of the function can be used in the function body, and it is not visible outside of the function body.

    Here is an example of a function expression:

    let greet = function(name) { console.log(`Hello, ${name}!`); };

    In this example, greet is a variable that is assigned a function that takes one parameter, name.

    Syntax of a Function Expression

    The syntax of a function expression is similar to the syntax of a function declaration, with a few key differences. The function keyword is used, followed by an optional name, a list of parameters in a pair of parentheses, and a pair of curly braces that delimit the body of the function. The entire function is then assigned to a variable.

    Anonymous Functions

    An anonymous function is a function that does not have a name. Anonymous functions are often used in function expressions. Here is an example of an anonymous function expression:

    let greet = function() { console.log('Hello, world!'); };

    In this example, the function does not have a name, and it does not take any parameters.

    Differences Between Function Declarations and Function Expressions

    There are a few key differences between function declarations and function expressions:

    • Function declarations are hoisted to the top of their containing scope, which means they can be used before they are declared. Function expressions are not hoisted, which means they cannot be used before they are defined.
    • Function expressions can be anonymous, while function declarations must have a name.
    • Because function expressions are assigned to variables, they can be used in places where only expressions are allowed, such as in the argument list of a function call.

    By understanding function expressions, you can write more flexible and powerful code in JavaScript.

    Test me
    Practical exercise
    Further reading

    Good morning my good sir, any questions for me?

    Sign in to chat
    Next up: Arrow functions