Published
The 'var' keyword in JavaScript is used to declare a variable. A variable is like a storage box where you can store different values. So, when you use 'var', you're basically telling JavaScript, 'Hey, I'm going to need a box to store something in later.'
You use 'var' by writing 'var' followed by the name you want to give to your 'box'. For instance, if you're making a game and need a variable to keep track of the player's score, you could call your variable 'score'.
var score;
Once you've declared your variable with 'var', you can put a value in it. Using the game score example, if everyone starts with a score of 0, you could put 0 in your 'score' variable. You do this using the equals (=) sign.
var score = 0;
You're not stuck with the initial value you put in your variable. You can update it. So, if a player just scored a point in your game, you could add 1 to your 'score' variable.
score = score + 1;
One thing to keep in mind with 'var' is its scope. The scope is the part of your code where your variable exists. If you use 'var' inside of a function, that variable can only be used in that function. But if you use 'var' outside of a function, that variable can be used anywhere in your code. However, this is a bit simplified, and the scope of 'var' can lead to some unexpected behavior, which is why many programmers prefer to use 'let' and 'const'.
The difference between 'var', 'let', and 'const' lies mainly in their scope and reassignment. While 'var' has function scope, 'let' and 'const' have block scope. Meaning, the 'let' and 'const' variables only exist within the block they were declared in. Also, 'var' and 'let' variables can be reassigned, while 'const' cannot. However, with JavaScript ES6 and onwards, it's recommended to use 'let' and 'const' over var for better code maintainability.
By reading this article, you've invested 1.75 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.
Imagine you are playing with your toy box. Before you can play with a toy, you need to take it out of the box. In JavaScript, 'var' is like a toy box. It's a way for us to store things, like numbers or words, so we can use them later. Just like how we can put a toy in a toy box and then take it out when we want to play with it, we can put a number or word in a 'var' and then take it out when we want to use it.
When we want to make a new toy box (or 'var'), we write 'var', then the name of the toy box, and then what we want to put in it. For example, we might write 'var myToy = 'teddyBear';' to make a new toy box named 'myToy' and put a teddy bear in it.
var myToy = 'teddyBear';
We can put lots of different things in a 'var'. We can put numbers in it, like 'var myNumber = 5;'. We can also put words in it, like 'var myWord = 'duck';'. We can even put sentences in it, like 'var mySentence = 'I love my teddy bear';'. Just like how a toy box can hold lots of different toys, a 'var' can hold lots of different things.
var myNumber = 5;
var myWord = 'duck';
var mySentence = 'I love my teddy bear';
Once we put something in a 'var', we can use it by using the name of the 'var'. Just like how you would say 'I want to play with my teddy bear' to get your teddy bear from your toy box, in JavaScript, you would say 'myToy' to use the teddy bear that you put in 'var myToy'.
console.log(myToy); // This will show 'teddyBear' on the screen.
Just like a toy box, 'var' in JavaScript is a handy way to hold and use things like numbers and words. If you can remember that 'var' is like a toy box, then you'll have a much easier time learning to code!
By reading this article, you've invested 1.72 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.
For more details, see What are let and const in JavaScript?
About author
Roman Y.
Senior Software Engineer at Nike
Certain articles only distributed to subscribers through email