1.

Explain the term “Scope” in JavaScript and write its different type.

Answer»

Managing the availability of variables or objects in an application is governed by the concept of scope. In JavaScript, there are two types of scope as follows:

Global Scope: A variable having global scope can be accessed from anywhere in the program. These variables that are DECLARED OUTSIDE of any FUNCTION can be accessed from any place in the program.

Example:

let scalerProgram = "DataScience"// code here can use scalerProgramfunction myScaler() {// code here can also use scalerProgram}

Local Scope: Variables with a local scope can only be accessed WITHIN the same function in which they are declared. Whenever a variable is declared inside a function, it becomes local to the function. As soon as a function begins, local variables are created and deleted when the function is executed.

Example:

// code here can NOT use scalerProgramfunction myScaler() { let scalerProgram = "DataScience"; // code here CAN use scalerProgram}// code here can NOT use scalerProgram


Discussion

No Comment Found