Skip to content

Latest commit

 

History

History
271 lines (225 loc) · 5.79 KB

File metadata and controls

271 lines (225 loc) · 5.79 KB

JavaScript Functions

JavaScript Functions

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.

Syntax of Creating a Function:

function functionName(parameters) {
    // Function code here
    return result; // Optional
}
  • function: The function keyword 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: The return statement 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 returns undefined).

Example 1: A Simple Function

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.

Example 2: Function Without Parameters

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.

Example 3: Function with Default Parameters

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."

Example 4: Function Expressions

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 Example 1

function greet() {
    console.log("Hello there");
}
greet();

Output

Hello there

Function Example 2

function greet() {
    console.log("Hello there");
}
greet();
console.log("After function call.");

Output

Hello there
After function call

Function Example 3

function greet() {
    console.log("Hello there");
}
greet();
greet();
greet();

Output

Hello there
Hello there
Hello there

Function arguments

Function Arguments Example 1

function greet(name) {
    console.log("Hello " + name);
}

greet();

Output

Hello undefined

Function Arguments Example 2

function greet(name) {
    console.log("Hello " + name);
}

greet("Jude");

Output

Hello Jude

Function Arguments Example 3

function greet(name) {
    console.log("Hello " + name);
}

greet("Jude");
greet("Jack");

Output

Hello Jude
Hello Jack

Passing Multiple Arguments

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

Return Value from a Function

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

JavaScript Built-in Functions

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

Assignment and Task

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.

Solution:

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

p4n Quiz

Q. What will be the output of the following code?

console.log(sqrt(9));
  1. 3
  2. 81
  3. error
  4. None of the above

Answer: 1