Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Functions in JavaScript | Section
JavaScript Essentials for Backend

Functions in JavaScript

Svep för att visa menyn

Functions help you organize and reuse code in JavaScript. A function is a named block of code that performs a specific task. You can define a function once and call it whenever needed.

There are three common ways to create functions: function declarations, function expressions, and arrow functions.

Function Declaration

A function declaration uses the function keyword followed by a name, parameters, and a block of code.

12345
function add(a, b) { return a + b; } console.log(add(3, 4)); // 7

Function Expression

A function expression assigns a function to a variable.

12345
const multiply = function(a, b) { return a * b; }; console.log(multiply(3, 4)); // 12

Arrow Functions

Arrow functions provide a shorter and more modern syntax. They are widely used in React and Next.js.

12345
const subtract = (a, b) => { return a - b; }; console.log(subtract(5, 2)); // 3

For simple expressions, you can return a value without writing return:

123
const divide = (a, b) => a / b; console.log(divide(10, 2)); // 5

Function Scope

Variables declared inside a function are only available within that function. This is called local scope.

123456
function greet(name) { const message = "Hello, " + name + "!"; return message; } console.log(greet("Sam")); // Hello, Sam!
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 8

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Functions in JavaScript

Functions help you organize and reuse code in JavaScript. A function is a named block of code that performs a specific task. You can define a function once and call it whenever needed.

There are three common ways to create functions: function declarations, function expressions, and arrow functions.

Function Declaration

A function declaration uses the function keyword followed by a name, parameters, and a block of code.

12345
function add(a, b) { return a + b; } console.log(add(3, 4)); // 7

Function Expression

A function expression assigns a function to a variable.

12345
const multiply = function(a, b) { return a * b; }; console.log(multiply(3, 4)); // 12

Arrow Functions

Arrow functions provide a shorter and more modern syntax. They are widely used in React and Next.js.

12345
const subtract = (a, b) => { return a - b; }; console.log(subtract(5, 2)); // 3

For simple expressions, you can return a value without writing return:

123
const divide = (a, b) => a / b; console.log(divide(10, 2)); // 5

Function Scope

Variables declared inside a function are only available within that function. This is called local scope.

123456
function greet(name) { const message = "Hello, " + name + "!"; return message; } console.log(greet("Sam")); // Hello, Sam!
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 8
some-alt