26JavaScript basics
JavaScript Functions
Easy20MIN
Using functions is an important part of programming. Functions are chunks of code that can be called upon to perform specific actions when needed. This makes the code modular and easy to read. Here are some examples of using functions in JavaScript:
Example 1:
JAVASCRIPT
1// Defining the function
2function greet(name) {
3 console.log("Hello " + name + "!");
4}
5
6// Calling the function
7greet("Matt"); // print "Hey Matt!"In this example, a function named "tervehdi" is defined. The function takes one parameter, "nimi". The purpose of the function is to print a greeting message to the console, using the given name. When the function is called with the parameter "Matti", the console will print "Hello Matti!".
1 / 3
Hakatemia Pro
Learn to hack — start here
Hundreds of interactive courses, virtual labs and CTF challenges in your browser. Start a free trial — no card required.