Javascript functions

Functions are a kind of subroutine that performs a specific task. Like every other programming language, JavaScript has its own rules for declaring functions. Some syntax may seem familiar; however, there are a few things to notice.

Syntax

The function keyword is placed before the name of the new function to indicate that a new function is being declared. Curly braces {} are used to indicate the function’s body, like most other languages.

function HelloWorld(args){
    // your code
}
Parts of a function
Parts of a function

Code

Console

Anonymous function

In JavaScript, we can make functions without a name called anonymous functions. In the example below, the variable HelloWorld can lose the function declaration and be replaced by any other value.

Console

Lambda Functions

Lambda functions are similar to anonymous functions, but they have more flexible features and syntax. An arrow function is another of this type of function.

var functionName = (parameters) => // function body
Console
Copyright ©2024 Educative, Inc. All rights reserved