JS Variable (변수)

4203 단어 jsjs

Variables label and store data in memory. There are only a few things you can do with variables:

  • Create a variable with a descriptive name.
  • Store or update information stored in a variable.
  • Reference or “get” information stored in a variable.
  • Variables hold reusable data in a program and associate it with a name.
    Variables are stored in memory.

It is important to distinguish that variables are not values; they contain values and represent them with a name


Creating variables

Pre ES6

  • Using var: allows hoisting, pre ES6.
  • Initialze variable: When a variable is declared with a value.
  • Variable names cannot be the same as keywords.
  • Variable names cannot start with number.
  • Never declare separate variables with the same name.

ES6

  • Using let: allows reassignment with different values, holds undefined without initialization.

  • When using let (and even var), we can declare a variable without assigning the variable a value.(Will hold undefined value) Meaning, let 이나 var 변수는 initilize 할필요 없이 변수 생성 가능.

  • Using const: cannot be reassigned. Must be innitialzed or assigned when declared.

  • It does not mean the value it holds is immutable—just that the variable identifier cannot be reassigned.

  • 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.

  • Global constants do not become properties of the window object, unlike var variables.


Mathemetical Assignment Operators

  • Mathmetical 연산자와 변수를 사용해 새로운 값을 연산하고 부여하는법:
let w = 4;
w = w + 1;
 
console.log(w); // Output: 5

//Use of built in mathmetical assignment operators
let w = 4;
w += 1;

console.log(w) // Output: 5

The Increment and Decrement Operator

*The increment operator will increase the value of the variable by 1. The decrement operator will decrease the value of the variable by 1. For example:

let a = 10;
a++;
console.log(a); // Output: 11

let b = 20;
b--;
console.log(b); // Output: 19

String concatenation(스트링 접합) & Interporlation(보간법)

let myPet = 'armadillo';
console.log('I own a pet ' + myPet + '.'); 

Interporation: In the ES6 version of JavaScript, we can insert, or interpolate, variables into strings using template literals.

One of the biggest benefits to using template literals is the readability of the code. Also not having to worry about escaping double quotes or quotes.


typeof operator

  • Checks data type of a variabl's value.
  • Returns string.
const unknown1 = 'foo';
console.log(typeof unknown1); // Output: string
 
const unknown2 = 10;
console.log(typeof unknown2); // Output: number
 
const unknown3 = true; 
console.log(typeof unknown3); // Output: boolean

Review

  • Variables hold reusable data in a program and associate it with a name.
    Variables are stored in memory.
  • The var keyword is used in pre-ES6 versions of JS.
  • let is the preferred way to declare a variable when it can be reassigned, and * const is the preferred way to declare a variable with a constant value.
  • Variables that have not been initialized store the primitive data type undefined.
  • Mathematical assignment operators make it easy to calculate a new value and assign it to the same variable.
  • The + operator is used to concatenate strings including string values held in variables
  • In ES6, template literals use backticks ` and ${} to interpolate values into a string.
  • The typeof keyword returns the data type (as a string) of a value.

좋은 웹페이지 즐겨찾기