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
12345function greet(name) { console.log("Hello, " + name + "!"); } greet("Alice");
Here’s what happens when you call greet("Alice")
:
- JavaScript looks for the function named
greet
. - It passes the string "Alice" as the argument for the parameter
name
. - Inside the function, the console.log statement concatenates "Hello, ", the value "Alice", and "!" to create the full greeting.
- The final message "Hello, Alice!" is then printed to the console.
Swipe to start coding
Рішення
Дякуємо за ваш відгук!
ninja.js
index.html
preset.js
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Сумаризуйте цей розділ
Пояснити код у file
Пояснити, чому file не вирішує завдання
Awesome!
Completion rate improved to 3.57
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
12345function greet(name) { console.log("Hello, " + name + "!"); } greet("Alice");
Here’s what happens when you call greet("Alice")
:
- JavaScript looks for the function named
greet
. - It passes the string "Alice" as the argument for the parameter
name
. - Inside the function, the console.log statement concatenates "Hello, ", the value "Alice", and "!" to create the full greeting.
- The final message "Hello, Alice!" is then printed to the console.
Swipe to start coding
Рішення
Дякуємо за ваш відгук!
Awesome!
Completion rate improved to 3.57ninja.js
index.html
preset.js