Contents
A variable is a container that stores a value or values. You can use variables to store data, like text strings, numbers, or arrays.
Checkout this video:
Introduction
In JavaScript, a variable can be declared using the var, let, or const keywords.
The var keyword is used to declare a variable in JavaScript. Variables declared with var are ignored in strict mode.
The let keyword is used to declare a block scope local variable in JavaScript. Block scope means that the variable is only available inside the block it was declared in, such as a for loop or an if statement.
The const keyword is used to create a read-only constant in JavaScript. A constant is a value that cannot be changed after it has been initialized.
What is a JavaScript Variable?
In JavaScript, variables are used to store data values. A variable can be declared with the keyword “var”. For example, the following code declares a variable named “x” and assigns the value 10 to it:
Rules for Naming a JavaScript Variable
As you learned in the previous lesson, a variable is like a storage container in which you can store a value. In JavaScript, there are rules that you must follow when naming a variable:
A variable name must start with a letter (a-z), an underscore (_), or a dollar sign ($).
After the first character, a variable name can contain any combination of letters (a-z), underscores (_), and dollar signs ($).
A variable name cannot contain any spaces.
A variable name cannot be a reserved word. (For example, you cannot use var or function as a variable name.)
JavaScript is case sensitive, which means that the names of variables can be different from each other as long as they have different capitalization. For example, the variables myName and MyName are two different variables.
Declaring a JavaScript Variable
To declare a JavaScript variable, you use the var keyword. For example, the following code declares a variable named price. The value of the variable is 2020:
var price=2020;
You can also use the var keyword to declare multiple variables in one statement. For example, the following code declares three variables in one statement:
var price=2020, quantity=3, totalPrice;
totalPrice = price * quantity;
Assigning a Value to a JavaScript Variable
One of the most common pieces of advice for beginners learning JavaScript is to avoid global variables. So, if global variables are bad, then what’s a variable? In short, a variable is simply a name that refers to a value. When you create a variable, you are essentially creating a slot in memory where you can store data. Data can be anything from simple strings and numbers to more complex data types like arrays and objects.
Creating a variable in JavaScript is simple: just use the var keyword followed by the name you want to give your variable. For example:
var myName = “Brian”;
This code creates a new variable called myName and assigns it the value “Brian”. From now on, whenever we use the myName variable, JavaScript will automatically substituted the value “Brian”. So, if we wanted to create a message that greets us by name, we could do something like this:
console.log(“Hello, ” + myName + “!”); // Outputs: Hello, Brian!
As you can see, using variables can make our code much simpler and more flexible. We could easily change the value of myName to something else and our code would still work just fine. Try it yourself!
Conclusion
By now, you should have a good understanding of how to declare variabes in JavaScript. Remember, there are three ways to do it: using the var keyword, using the let keyword, or using the const keyword. Each approach has its own benefits and drawbacks, so choose the one that makes the most sense for your particular situation.