Home > Web Front-end > JS Tutorial > body text

Explore the concept of JavaScript function scope and the different types of JavaScript functions

WBOY
Release: 2023-09-07 14:41:02
forward
1050 people have browsed it

探索 JavaScript 函数作用域的概念和不同类型的 JavaScript 函数

In JavaScript, different scopes allow us to access functions and variables from different locations in the code. We will learn about function scope in this tutorial. Additionally, we will explore the different types of function expressions in JavaScript.

Function scope

When we create a new function in JavaScript, it also creates the scope of that specific function. This means that whatever variable we declare inside a function or define a nested function within it, we can access it only within the function block.

If we try to access a variable defined inside a function block from outside the function, a reference error will occur.

grammar

You can follow the following syntax to define functions and understand function scope.

function  function_name(){
   var variable = 10; // variable has a function scope.
}
let variable1 = variable; 
// we can't access the variable here as it has a function scope.
Copy after login

In the above syntax, you can see that we cannot access the variables outside the function because the function block restricts it.

Example 1

In this example, we created the sample function and defined the variables inside the function with block scope. If we try to access a variable defined inside the Sample() function from outside the function, JavaScript will throw a reference error.

<html>
<body>
   <h2>Function Scope in JavaScript</h2>
   <div id="output"></div>
   <script>
      let output = document.getElementById("output");
      // defining the function
      function sample() {
         //  variables with function scope.
         var website = "TutorialsPoint!";
         var language = "JavaScript";
         output.innerHTML +=
            "You are learning the " +
            language +
            " programming language on " +
            website +
            " </br>";
      }
      sample();
      // we can't access language and website variables here.
   </script>
</body>
</html>
Copy after login

Example 2

We define nested_function() in the example function in this example. We cannot call nested_funciton() outside the sample() function because nested_function is within the scope of the sample function.

<html>
<body>
   <h2>Function sSope in JavaScript</h2>
   <div id="output"></div>
   <script>
      let output = document.getElementById("output");
      function sample() {
         //  variables with function scope.
         var num = 10;
         function nested_function() {
            // num variables also can be accessed here as nested_function() is
            //inside the scope of sample function
            var string = "JavaScript";
            output.innerHTML +=
               "The value of the num variable is " + num + "<br/>";
            output.innerHTML +=
               "The value of the string variable is " + string + "</br>";
         }
         //  we can call the nested_function() inside the sample() function only
         nested_function();
         // we can't access the string variable here as the scope of 
         //nested_function bounds it
      }
      sample();
   </script>
</body>
</html>
Copy after login

Different types of functions in JavaScript

According to the definition and declaration of the function, there are many types of functions. We will study them one by one here.

Normal function

Normal functions are functions commonly used by all JavaScript developers. We can define regular functions using the function name followed by the function keyword.

Regular functions remain at the top of the function's current scope. This means we can call the function before defining it, but it should be defined after execution.

grammar

Define regular functions according to this syntax.

function function_name(){
   // function body
}
Copy after login

In the above syntax, we use the function keyword to define the function. ‘function_name’ is the name of the function, and we can write the code for the function body within curly brackets.

Function expression

Function expressions also work similarly to ordinary functions. However, the difference is that it doesn't have any name and we need to store the function expression in a variable. We can use the identifier to call the function that stores it.

JavaScript evaluates function expressions step by step. Therefore, it is not hoisted to the top of the scope, so we cannot call it before declaring it.

grammar

Define function expressions according to this syntax.

var function_identifier = function () {
   // function body
}
function_identifier();
Copy after login

In the above syntax, we have defined a function without a name using only the function keyword and stored it in the function_identifier variable. Additionally, users can see how we use function_identifier to call function expressions.

Arrow function

Arrow functions were introduced in ES6, the last major revision of JavaScript in 2015. It is a shorter syntax for defining functions without a function name. Also, it is called expressions and anonymous functions because it does not contain their identities.

grammar

Define arrow functions according to this syntax.

var function_identifier =  (params) => {
   // function body
}
function_identifier(arguments);
Copy after login

In the above syntax, we store the arrow function expression in function_identifier. Furthermore, we passed the arguments and arguments inside the arrow function while calling the function using the function_identifier variable.

Close function

We can create nested functions in JavaScript and use child functions to access parent function variables. Since the child function contains access to all variables defined in the scope of the parent function, we can call the child function a closure function.

grammar

function parent() {
   // variables of the parent
   var child = () => {
      // variables of child
      // access variables of the parent
   };
   return child;
}
var child = parent();
child();
Copy after login

In the above syntax, we can access all variables of the parent function inside the child function, and the parent function returns the child function. Therefore, we can call a child function indirectly from outside the parent function, even if it is defined within the scope of the parent function.

Constructor

grammar

We can use constructors to create objects.

function constructor(property){
   this.property = property
}
var string = new constructor("value");
Copy after login

In the above syntax, we create the object of the constructor.

We learned how function scope for nested functions works through two examples in this tutorial. Additionally, we learned about the different types of functions. Additionally, there are some other types of functions such as recursive or callback functions that users can explore on the internet.

The above is the detailed content of Explore the concept of JavaScript function scope and the different types of JavaScript functions. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!