Sobes.tech
Junior

Millised on kaks viisi funktsioonide deklareerimiseks JavaScriptis?

sobes.tech AI

Vastus AI-lt

There are two main ways to declare functions:

  1. Function Declaration:

    // Function declaration with the name 'sum'
    function sum(a, b) {
      return a + b;
    }
    
  2. Function Expression:

    // Function expression assigned to the variable 'subtract'
    const subtract = function(a, b) {
      return a - b;
    };
    

There is also a variant of the function expression - Arrow Function:

// Arrow function assigned to the variable 'multiply'
const multiply = (a, b) => a * b;