In programming, variables are like little boxes where we can store information. We can name these boxes anything we want and put different types of information inside them. In JavaScript, there are eight different data types that we can use to store information in variables: strings, numbers, booleans, arrays, objects, functions, undefined, and null. Thus, look at variables in js.
In this blog post, we’ll take a closer look at each of these data types and learn how to create variables in JavaScript. By the end of this post, you should have a good understanding of how to create and use variables in your own programs.
Creating Variables in JavaScript
Before we can start using variables in our programs, we need to learn how to create them. In JavaScript, there are two ways to create variables: using the var keyword or using the let keyword. Let’s take a closer look at each of these methods.
Using the var Keyword
The var keyword is used to create variables that are global in scope. This means that the variable can be used anywhere in your program.
Such as
var myname = “John Doe”;
console.log(myName); // John Doe
As you can see from the example above, we created a variable called myName using the var keyword and assigned it the value “John Doe”. We were then able to access the value of the variable by typing its name into the console.
You should also be aware that variables created with the var keyword are hoisted to the top of their scope. This means that they can be used before they’re even declared in your code. For example:
console.log(myName); // undefined
var myName = “John Doe”;
As you can see from the example above, even though we declared our myName variable after logging it to the console because it was hoisted to the top of its scope (i.e., the entire program), it was still accessible.
Variables declared with the var keyword are also treated as if they’re declared inside a function, even if they’re not actually inside one. This means that if you declare a variable with var inside a for loop or an if statement, it will be accessible outside of that loop or statement once execution reaches the end of it. For example:
for(var i = 0; i < 10; i++) { console.log(i); // 0 1 2 3 4 5 6 7 8 9 } console.log(i); // 10
As you can see from the example above, even though we declared our i variable inside a for loop, because it was treated as if it was declared inside a function (due to hoisting), it was still accessible outside of the loop once execution reached the end of it. The same would be true if we declared I inside an if statement or any other kind of control flow statement (e.g., while loops).
Using let Keyword The let keyword is used to create block-scoped variables—variables that are only available inside the block (usually a curly brace {} set) where they’re declared and initialized. For example:
{ let myName = “John Doe”; console.log(myName); // John Doe} console.log(myName); // ReferenceError: my name is not defined
As you can see from the example above, because our myName variable was only available inside its enclosing block (denoted by curly braces {}), when we tried accessing it outside of that block, we got an error saying that it was not defined. So why would you want to use let over var? The main reason is that lets give you more control over your code.