JavaScriptVariable Scope

The scope of a variable is the region of your program in which it is defined. Traditionally,

JavaScript defines only two scopes-global and local.

Global Scope: A variable with global scope can be accessed from within any part of the JavaScript code.

Local Scope: A variable with a local scope can be accessed from within a function where it is declared.

Example: Global vs. Local Variable

var num=10

function test()

{

var num=100

console.log("value of num in test() "+num)

}

console.log("value of num outside test() "+num)

test()

The following output is displayed on successful execution.

value of num outside test() 10

value of num outside test() 100

要查看或添加评论,请登录

社区洞察

其他会员也浏览了