Javascript Notes: 03 – Functions
Functions are one of the fundamental building blocks in Javascript. A function is basically a set of statements that performs a task or calculates a value.
Example 1:
function greet() {
console.log(‘Hello World’);
}
greet();
Example 2:
//Name is a parameter of the greet function
function greet(name) {
console.log(‘Hello’ + name);
}
//John is an argument to greet function
greet(‘John’);
greet(‘Mary’);
Example 3:
//Performing a task
function greet(name, lastName) {
console.log(‘Hello ‘ + name + ‘ ‘ + lastName);
}
greet(‘John’, ‘Smith’);
Types of functions
Example 4:
//Calculating a value
function square(number) {
return number * number;
}
let number = square(2);
console.log(number);