Function and Expressions In JavaScript

Function and Expressions In JavaScript

With the help of examples, you will learn about JavaScript functions and function expressions in this course.

The fundamental distinction between a function expression and a function declaration is the function name, which in function expressions can be removed to generate anonymous functions.

An IIFE (Immediately Invoked Function Expression) is a function expression that runs as soon as it is defined.

JavaScript Function

A function is a piece of code that accomplishes a specified goal.

Assume you need to write software to draw and colour a circle. To fix this problem, you can write two functions:

a function for drawing a circle
a circle colouring function
By breaking down a complex problem into smaller bits, you can make your programme more understandable and reusable.

JavaScript also includes a plethora of built-in functions. Math. sqrt(), for example, is a function that calculates the square root of a number.

This course will teach you about user-defined functions.

Creating a Function

The syntax to declare a function is:

function nameOfFunction () {
    // function body   
}
  • The function keyword is used to declare a function.
  • The fundamental guidelines for naming a function are similar to those for naming a variable. It is preferable to give your function a descriptive name. For example, if a function is used to add two integers, the function name may be added or add numbers.
  • The function body is written within.

As an example,

// declaring a function named greet()
function greet() {
    console.log("Hello there");
}

Using a Function

In the preceding programme, we declared a function called greet (). We must call that function in order to utilise it.

Here’s how to use the greet() function mentioned above.

// function call
greet();

Example 1: Display a Text(Function and Expressions In JavaScript )

// program to print a text
// declaring a function
function greet() {
    console.log("Hello there!");
}

// calling the function
greet();

Output

Hello there!

Function Parameters

A parameterized function can also be declared. A parameter is a value that is supplied to a function when it is declared.

Example 2: Function with Parameters(Function and Expressions In JavaScript )

// program to print the text
// declaring a function
function greet(name) {
    console.log("Hello " + name + ":)");
}

// variable name can be different
let name = prompt("Enter a name: ");

// calling function
greet(name);

Output

Enter a name: Simon
Hello Simon 🙂

The greet function is declared with a name parameter in the preceding programme. A name is requested from the user. When the function is invoked, an argument is sent into it.

Note: When a value is passed when declaring a function, it is referred to as a parameter. When the function is invoked, the value given is referred to as the argument.

Example 3: Add Two Numbers(Function and Expressions In JavaScript )

// program to add two numbers using a function
// declaring a function
function add(a, b) {
    console.log(a + b);
}

// calling functions
add(3,4);
add(2,9);

Output

7
11

The add function is used in the preceding programme to find the sum of two values.

  • The function is defined as having two parameters, a and b.
  • The function is called by its name and two arguments, 3 and 4 in one case and 2 and 9 in the other.

You should be aware that you can call a function as many times as you wish. You can define one function and then call it with different arguments many times.

Function Return

To return the value of a function call, use the return statement.

The return statement indicates that the function has come to an end. Any code following return is ignored.

If no value is returned, the function returns undefined.

The operation of a JavaScript function with a return statement

Example 4: Sum of Two Numbers(Function and Expressions In JavaScript )

// program to add two numbers
// declaring a function
function add(a, b) {
    return a + b;
}

// take input from the user
let number1 = parseFloat(prompt("Enter first number: "));
let number2 = parseFloat(prompt("Enter second number: "));

// calling function
let result = add(number1,number2);

// display the result
console.log("The sum is " + result);

Output

Enter first number: 3.4
Enter second number: 4
The sum is 7.4

The function in the preceding programme returns the sum of the numbers using the return statement. That value is then saved in the result variable.

Benefits of Using a Function

  • The function allows the code to be reused. It can be declared once and used several times.
  • Each little task is split into a function, which simplifies the software.
  • The function improves readability.

Function Expressions

Functions can also be defined as expressions in Javascript. As an example,

// program to find the square of a number
// function is declared inside the variable
let x = function (num) { return num * num };
console.log(x(4));

// can be used as variable value for other variables
let y = x(3);
console.log(y);

Output

16
9

Variable x is used to store the function in the preceding programme. The function is handled as an expression in this case. And the variable name is used to call the function.

An anonymous function is the function described above.

Note: JavaScript expressions in ES2015 are written as arrow functions. Later tutorials will teach you more about them.

Leave a Reply