A function in JavaScript is a reusable block of code that performs a specific task or a set of tasks. Functions are a fundamental building block in JavaScript and are used to encapsulate and organize code for better maintainability and reusability.
function functionName(parameters) {
// Function code here
return result; // Optional
}-
function: Thefunctionkeyword is used to declare a function. -
functionName: This is the name of the function, which should be a valid identifier. It is followed by parentheses. -
parameters: Inside the parentheses, you can specify zero or more parameters (input values) that the function accepts. These are separated by commas. -
Function code: This is the code that gets executed when the function is called. It can include any number of statements and calculations. -
return: Thereturnstatement is optional. If used, it specifies the value to be returned as the result of the function. The function can return a single value, an object, or nothing (in which case it returnsundefined).
Let's create a simple function that adds two numbers and returns the result.
function addNumbers(a, b) {
return a + b;
}
const sum = addNumbers(5, 3);
console.log("The sum is: " + sum);Output:
The sum is: 8
This function addNumbers accepts two parameters, a and b, and returns their sum.
Here's a function that doesn't take any parameters and simply prints a message.
function sayHello() {
console.log("Hello, world!");
}
sayHello();Output:
Hello, world!
This function sayHello doesn't require any parameters and just logs a message.
In JavaScript, you can set default values for function parameters. If a value is not provided when the function is called, it will use the default value.
function greet(name = "Guest") {
console.log("Hello, " + name + "!");
}
greet("Alice"); // Outputs: Hello, Alice!
greet(); // Outputs: Hello, Guest!In this example, the greet function has a default parameter name set to "Guest." If you provide a name when calling the function, it uses that name; otherwise, it defaults to "Guest."
You can also define functions using function expressions. Function expressions can be assigned to variables and used as values.
const multiply = function(a, b) {
return a * b;
};
const result = multiply(4, 3);
console.log("The result is: " + result);Output:
The result is: 12
In this example, we define a function using a function expression and assign it to the multiply variable.
JavaScript functions are powerful tools for organizing and reusing code. They can take parameters, return values, have default values for parameters, and be assigned to variables using function expressions. Functions are essential for structuring complex code and making your JavaScript code more modular and maintainable.
function greet() {
console.log("Hello there");
}
greet();Output
Hello there
function greet() {
console.log("Hello there");
}
greet();
console.log("After function call.");Output
Hello there
After function call
function greet() {
console.log("Hello there");
}
greet();
greet();
greet();Output
Hello there
Hello there
Hello there
function greet(name) {
console.log("Hello " + name);
}
greet();Output
Hello undefined
function greet(name) {
console.log("Hello " + name);
}
greet("Jude");Output
Hello Jude
function greet(name) {
console.log("Hello " + name);
}
greet("Jude");
greet("Jack");Output
Hello Jude
Hello Jack
function addNumbers(n1, n2) {
let result = n1 + n2;
console.log("The sum is " + result);
}
let number1 = 6.6;
let number2 = 2.5;
addNumbers(number1, number2);Output
The sum is 9.1
function addNumbers(n1, n2) {
let result = n1 + n2;
return result;
}
let sum = addNumbers(6.6, 2.5);
console.log("The sum is " + sum);Output
The sum is 9.1
const num = 9;
const squareRoot = Math.sqrt(num);
console.log(`The square root of ${num} is ${squareRoot}`);Output
The square root of 9 is 3
Can you create a program that can tell us if someone is eligible to vote? For this, Create a function canVote that accepts the age of the person. If the age is above 18, then return true else false Call the function for ages 17, 20 and 65 to verify if it is working correctly or not.
function canVote(age) {
if (age > 18) {
return true;
} else {
return false;
}
}
const result1 = canVote(17);
console.log(result1);
const result2 = canVote(20);
console.log(result2);
const result3 = canVote(65);
console.log(result3);Output
false
true
true
Q. What will be the output of the following code?
console.log(sqrt(9));- 3
- 81
- error
- None of the above
Answer: 1