DEV Community

shinyo627
shinyo627

Posted on

JS Variable

In programming, a variable is a container for a value. You can think of variables as little containers for information that live in a computer’s memory. ex) such as a username, account number, or even personalized greeting can then be found in memory.

Variables also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves.

In short, variables label and store data in memory. 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.

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

  • If we don’t assign a value to a variable declared using the let keyword, it automatically has a value of undefined.
  • We can reassign the value of the variable.

However, a const variable cannot be reassigned because it is constant. If you try to reassign a const variable, you’ll get a TypeError.

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.

Tip: If you’re trying to decide between which keyword to use, let or const, think about whether you’ll need to reassign the variable later on.

If you do need to reassign the variable use let, otherwise, use const.


String Interpolation - 문자열 보간을 이용해 string 값

  • One of the biggest benefits to using template literals is the readability of the code. Using template literals, you can more easily tell what the new string will be. You also don’t have to worry about escaping double quotes or single quotes.

typeof returns a string describing the data type of instances/value.

Top comments (0)