Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Using Functions | Functions
New Script JS
course content

Contenu du cours

New Script JS

New Script JS

1. Basic Controls
2. Functions
3. Loops
4. If-Else Statements
5. Challenges

book
Using Functions

Why Use Functions?

Repeating code makes programs long and hard to maintain. Functions let you group commands into a reusable block, reducing repetition and making your code cleaner.

How to call Functions?

To call a function, simply use its name followed by parentheses, placing any required arguments inside those parentheses.

In the example below, the greet function is called on line 5 by adding parentheses to its name and passing "Alice" as the name argument.

A Simple Function Example

12345
function greet(name) { console.log("Hello, " + name + "!"); } greet("Alice");
copy

Here’s what happens when you call greet("Alice"):

  1. JavaScript looks for the function named greet.
  2. It passes the string "Alice" as the argument for the parameter name.
  3. Inside the function, the console.log statement concatenates "Hello, ", the value "Alice", and "!" to create the full greeting.
  4. The final message "Hello, Alice!" is then printed to the console.
Tâche

Swipe to start coding

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 1
js

ninja.js

html

index.html

js

preset.js

toggle bottom row

book
Using Functions

Why Use Functions?

Repeating code makes programs long and hard to maintain. Functions let you group commands into a reusable block, reducing repetition and making your code cleaner.

How to call Functions?

To call a function, simply use its name followed by parentheses, placing any required arguments inside those parentheses.

In the example below, the greet function is called on line 5 by adding parentheses to its name and passing "Alice" as the name argument.

A Simple Function Example

12345
function greet(name) { console.log("Hello, " + name + "!"); } greet("Alice");
copy

Here’s what happens when you call greet("Alice"):

  1. JavaScript looks for the function named greet.
  2. It passes the string "Alice" as the argument for the parameter name.
  3. Inside the function, the console.log statement concatenates "Hello, ", the value "Alice", and "!" to create the full greeting.
  4. The final message "Hello, Alice!" is then printed to the console.
Tâche

Swipe to start coding

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 1
Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
We're sorry to hear that something went wrong. What happened?
some-alt