JavaScript variables are storage locations that can be classified as either global or local. Global variables are accessible throughout the code, while local variables are confined to the function in which they are declared. Variables can be declared using 'let', 'const', or 'var', with each having different scoping and hoisting behaviors.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views
Variable in Javascript
JavaScript variables are storage locations that can be classified as either global or local. Global variables are accessible throughout the code, while local variables are confined to the function in which they are declared. Variables can be declared using 'let', 'const', or 'var', with each having different scoping and hoisting behaviors.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6
Variable in javascript
• Variable simply mean a name of storage location.
• There are two types of variables in JavaScript : local variable and global variable TYPES OF JAVASCRIPT VARIABLES • Global Variables • Variables declared out of any function are called global variables. They can be accessed anywhere in the JavaScript code, even inside any function. • Local Variables • Variables declared inside the function are called local variables of that function. They can only be accessed in the function where they are declared but not outside. Points to Remember • Variables can be defined using let keyword. Variables defined without let or var keyword become global variables. • Variables should be initialized before accessing it. Unassigned variable has value undefined. • JavaScript is a loosely-typed language, so a variable can store any type value. • Variables can have local or global scope. Local variables cannot be accessed out of the function where they are declared, whereas the global variables can be accessed from anywhere. Variables Declaration in JS • There are three ways to define a variable in JavaScript − • let − The JavaScript Let keyword introduced in 2015 allows us to define block scoped variables. Variables declared using let are not hoisted. <script> let x = 10; let y = 6; let z = x + y; alert("The value of z is: " + z); </script> • const − The const declarations create variables that cannot be reassigned to some other value or redeclared later. <script> const x = 5; const y = 25; const z = x + y; alert("The value of z is: " + z); </script> • var − The JavaScript var keyword is used for creating function scoped variables and are hoisted. • Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. <script> var x = 10; var y = 20; var z = x + y; Alert("The value of z is: " + z); </script>