Найти в Дзене

Variables in JavaScript

In short, variables label and store data in memory. It is important to distinguish that variables are not values; they contain values and represent them with a name. There are only a few things you can do with variables: There are a few general rules for naming variables: The let keyword signals that the variable can be reassigned a different value. Another concept that we should be aware of when using let (and even var) is that we can declare a variable without assigning the variable a value. In such a case, the variable will be automatically initialized with a value of undefined. Constant variables must be assigned a value when declared. If you try to declare a const variable without a value, you’ll get a SyntaxError. The increment operator (++) will increase the value of the variable by 1. The decrement operator (--) will decrease the value of the variable by 1. Just like the previous mathematical assignment operators (+=, -=, *=, /=), the variable’s value is updated and assigned
JavaScript Tutorial: Learn JavaScript For Free | Codecademy

In short, variables label and store data in memory. It is important to distinguish that variables are not values; they contain values and represent them with a name.

There are only a few things you can do with variables:

  1. Create a variable with a descriptive name.
  2. Store or update information stored in a variable.
  3. Reference or “get” information stored in a variable.

There are a few general rules for naming variables:

  • Variable names cannot start with numbers.
  • Variable names are case sensitive, so myName and myname would be different variables. It is bad practice to create two variables that have the same name using different cases.
  • Variable names cannot be the same as keywords. For a comprehensive list of keywords check out MDN’s keyword documentation.

The let keyword signals that the variable can be reassigned a different value.

-2

Another concept that we should be aware of when using let (and even var) is that we can declare a variable without assigning the variable a value. In such a case, the variable will be automatically initialized with a value of undefined.

Constant variables must be assigned a value when declared. If you try to declare a const variable without a value, you’ll get a SyntaxError.

-3

The increment operator (++) will increase the value of the variable by 1. The decrement operator (--) will decrease the value of the variable by 1. Just like the previous mathematical assignment operators (+=, -=, *=, /=), the variable’s value is updated and assigned as the new value of that variable.

The + operator can be used to combine two string values even if those values are being stored in variables:

-4

In the ES6 version of JavaScript, we can insert, or interpolate, variables into strings using template literals. Check out the following example where a template literal is used to log strings together:

-5

While writing code, it can be useful to keep track of the data types of the variables in your program. If you need to check the data type of a variable’s value, you can use the typeof operator.

-6