Identifiers
- a letter(A-z, case sensitive)
- a dollar sign($)
- an underscore(_)
- hypen is NOT allowed
- NO start with numbers, NO include space
- start with lower case or $ or _ , use camelCase
Variables
declear –> recall
하나의 변수(a) 값을 다른 변수(b)에 활용해도 link는 지속되지 않아 변수(a)의 값이 변해도 변수(b)는 영향 없음
declare
- must identified with unique names
- one statement, many variables
let name = "Mike", age = 15, gender="M"; - declare without value
- undefined
- re-declaring
- can
var name = "Mina"; var name; - cannot
let name = "July"; let name; //error
- can
- using dollar sign
- concern as an alias for the main function in jQuary
- using underscore
- concern as an alias for private(hidden) variables
const (~2015)
- value should not be changed
- type should not be changed (Arrays and Objects)
- cannot be redeclared
- cannot be reassigned
- have block scope (cf. global scope, function scope)
- does not define a constant value, defines a constant reference to a value
- can change elements of constant array
- can change the properties of constant object
const person = {name : "June", age : 16, height:169} person.height = 175; //allowed person = {name : "May", age:45, height:180}; //not allowed
let (~2015)
- value can be changable
- cannot be redeclared
- must be declared before use
- have block scope
- in a block declare ONCE
let x = 10; //x=10 { let x = 2; //x=2 let x = 8; //not allowed } //x=10
- in a block declare ONCE
var
- changable
- only if you MUST support old browsers
- redeclearing problem
var x = 10; //x=10 { var x = 2; //x=2 } //x=2 - hoisted to the top (can use the variable before it is declared)
Comparision
| | Scope | Redeclare | Reassign | Hoisted | Binds this | | —– | —– | ——— | ——– | ——- | ———- | | var | N | Y | Y | Y | Y | | let | Y | N | Y | N | N | | const | Y | N | N | N | N |
하나의 변수(a) 값을 다른 변수(b)에 활용해도 link는 지속되지 않아 변수(a)의 값이 변해도 변수(b)는 영향 없음