Java script c-2 variables
i)let ii)const iii)var Let,var -almost same let-It works in {} only var-It works in globally.outoff {} const-we cannot change the value. Eg.amazon order limit. Program: clickme </button) addnumbers - function name clickme - button name Program: let a=100 function addnumbers() console.log(a) Op: 100 var name="kavin" var name="Praveen" var name="naresh" console.log(name) Op: naresh…
In the world of JavaScript programming, there are three types of variables: let, const and var. Each of these variables has its own set of rules and uses.
Let variables can only be declared within curly braces {}. This means they are only accessible within that scope. On the other hand, var can be declared both within and outside curly braces. This means they can be accessed from outside the scope in which they were declared.
Const variables are similar to let variables in that they can only be declared within curly braces {}. However, there is a key difference between the two. Once a const variable is assigned a value, it cannot be changed. This makes const variables useful when you want to ensure a value remains constant throughout your program.
Despite the differences, var has some demerits. For example, it initializes multiple reference values with the same name, which increases the chance of creating bugs in your code. Additionally, var variables follow function scoping, meaning they can only be accessed within the function they were declared in. However, if the closing console.log statement is removed, the final value of var a will be 15.
In conclusion, while all three types of variables in JavaScript serve the purpose of storing values, they each have their own unique rules and uses. Let variables are restricted to within curly braces {}, const variables cannot be changed once assigned a value, and var variables have function scoping and can be accessed outside their declared scope. Understanding these differences is crucial for effective JavaScript programming.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.