In JavaScript, we use variables to store and track information.
Variables are one of the most important concepts in programming. A variable is a way of storing and keeping track of information in a program so that it can be later used and manipulated.
For example, the code console.log() we just talked about is accepting variables inside the parentheses.
Create and Change Variables
A variable is like a box that you labeled with a unique name. By assigning a variable, you put something inside the box. You can also reassign it, which means you empty the box and put something new in the box. Even though the contents of the box changes, it's still always the same box with the unique label you gave it.
Declare a variable
To use a variable, we need to declare a variable first. This is the step of labelling your box.
Use the var, const, or let keyword followed by the variable name to declare a variable.
*These 3 keywords are different when it comes to reassigning variables.
Don’t forget to put a semicolon at the end of each JavaScript statement.
Name a variable
There are a few rules you'll need to follow when creating a variable:
JavaScript has somereserved keywords that you can't use for a variable name. Using them can cause a syntax error.
Names can't start with a number.
Names can only contain letters, numbers, and the $ and _ characters.
When you want to create a variable name with multiple words, there’s a convention to use camel case, which is to capitalize every word after the first word.
Create and Change Variables
Declare a variable
var studentName;
const pi;
let message;
Name a variable
Assign a variable
studentName = "Mengyi";
pi = 3.14;
message = "hello";
var studentName = "Mengyi";
const pi = 3.14;
let message = "hello";
Use a variable
let message = "hello";
console.log(message);