HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaScriptVariables (let / const)Data TypesOperatorsStringsArraysObjectsConditions (if / else)Loops (for / while)FunctionsArrow Functions (=>)Array Methods (map / filter / reduce)
REFERENCE
📖 Overview
🖥️ Output & Console
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🗂️ Object
🔁 Condition & Loop
🧩 Function
functionArrow function (=>)Default parameter...rest / ...spread
🔄 Type & Conversion
JavaScript›REFERENCE›Function
🧩

Function

Defining and reusing your own commands.

CommandDescription
functionDefines a function.
Arrow function (=>)Short function syntax.
Default parameterGives a parameter a default value.
...rest / ...spreadGathers a variable number of arguments / expands an array.

function

Try it yourself →

Defines a function.

Syntax

function name(param) { return value; }

Description

Binds repetitive work to a name; takes data via parameters and returns a result with return.

Example

function square(x) {
  return x * x;
}
console.log(square(5));

Expected output

25

When to use?

Preventing code repetition, breaking the program into parts.

Common mistakes

  • ⚠️ If you don't write return, the function returns undefined.

Arrow function (=>)

Try it yourself →

Short function syntax.

Syntax

const name = (p) => expression;

Description

Short function definition without writing function. If there is a single expression, { } and return are not needed. Frequently used in places like map/filter.

Example

const square = x => x * x;
console.log(square(6));

Expected output

36

When to use?

Short helpers, arguments for array methods.

Default parameter

Try it yourself →

Gives a parameter a default value.

Syntax

function name(x, y = default) { }

Description

If no argument is provided, the default is used.

Example

function greet(name, message = "Hello") {
  return `${message} ${name}`;
}
console.log(greet("Alex"));

Expected output

Hello Alex

When to use?

Optional settings, frequently repeated arguments.

...rest / ...spread

Try it yourself →

Gathers a variable number of arguments / expands an array.

Syntax

function name(...args) { }

Description

In function definition, ...args gathers remaining arguments into an array. In a call, ...array spreads the array into individual elements (spread).

Example

function addUp(...numbers) {
  return numbers.reduce((t, x) => t + x, 0);
}
console.log(addUp(1, 2, 3, 4));

Expected output

10

When to use?

Indefinite number of arguments, merging arrays.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code