TIL13 - JS - Variables

Peter D Lee·2020년 8월 26일
1

JavaScript

목록 보기
2/19

The 3 Keywords for Declaring a Variable

*In Javascript, there are 3 keywords you can use to declare a varible:

  • var
  • let
  • const

Starting with ES6, the let and const keywords are used, making the var keyword almost obsolete in usage. Using let and const is preferred.

When a variable is assigned a value with one of the above keywords, the variable is said to be initialized with the assigned value

let

  • Variables assigned a value using the let keyword can be re-assigned to a different value
  • You can declare a variable with let without assigning a value to the variable name
    > In this case, the variable is automatically initialized with a default value of undefined.

The var keyword functions similarly to let

const

  • Variables declared with const keyword cannot be re-assigned
    > const stands for "constant"
    > trying to re-assign a value to a const declared variable yields TypeError
  • A const variable must be initialized with a value
    > declaring a const variable without a value yields SyntaxError

0개의 댓글