09
11

변수 선언

자바스크립트에서 변수를 선언할 수 있는 것은 let하나이다. let은 ES6에 추가되었다.

 

ex)

//1. Use strict
// added in ES 5
//use this for Valina Javascript.
'use strict';

//2. Variable
//let(added in ES6)
let name = 'han';
console.log(name);
name = 'hello';
console.log(name);

 

예제 실행 화면

 

{} 안에 변수를 선언하면 그 범위 내에서 사용이 가능하다. 전역으로 사용되는 변수는 웹이 실행될 때부터 끝날 때까지 메모리를 사용하기 때문에 최소한으로 사용하는 것이 좋다.

 

ex)

//2. Variable
//let(added in ES6)
let globalName = 'global name';
{
    let name = 'han';
    console.log(name);
    name = 'hello';
    console.log(name);
    console.log(globalName);
}
console.log(name);
console.log(globalName);

 

예제 실행 화면

 

그 전에는 var를 사용했지만 지금은 사용하지 않는다. var는 선언 전에 값을 할당할 수도, 출력할 수도 있다.

(var hoisting : 어디에서 선언했는지 상관없이 가장 위로 올려준다.)

또한 블록을 무시하기 때문에 어디에서든 값을 불러올 수 있다.

 

ex)

//2. Variable
//let(added in ES6)
let globalName = 'global name';
{
    let name = 'han';
    console.log(name);
    name = 'hello';
    console.log(name);
    console.log(globalName);
}
console.log(name);
console.log(globalName);

//var (don't ever use this!)
//var hoisting (move declaration from bottom to top)
//has no block scope
{
    age = 4;
    var age;
}
console.log(age);

 

예제 실행 화면

 

Constants

값을 선언함과 동시에 값을 바꿀 수 없는 데이터 타입을 의미한다.(보안상의 이유, 스레드가 동시에 값을 변경함을 방지, 실수를 방지)

 

//3.constants
//favor immutable data type always for a few reason:
//  -security
//  -thread safety
//  -reduce human mistakes
const dayInWeek = 7;
const maxNumber = 5;

 

데이터 타입

  • Primitive, single item : number(숫자), string, boolean, null, undefined, symbol
  • object, box container
  • function, first-class function(함수의 파라미터에 함수를 넣을 수 있음)

자바스크립트에서는 let으로 선언할 때 다이나믹하게 타입이 결정됨.

 

ex)

const count = 17; // integer
const size = 17.1; // decimal number
console.log(`value: ${count}, type: ${typeof count}`);
console.log(`value: ${size}, type: ${typeof size}`);

//number - speical numberic values : infinity, -infinity, Nan
const infinity = 1/0;
const negativeInfinity = -1/0;
const nAn = 'not a number' / 2;
console.log(infinity);
console.log(negativeInfinity);
console.log(nAn);

 

예제 실행 화면

 

ex)

//bigInt(fairly new, don't use it yet)
const bigInt = 1234567890123456789012345678901234567890n; // over(-2**53 ~ 2**53)
console.log(`value: ${bigInt}, type: ${typeof bigInt}`);
Number.MAX_SAFE_INTEGER;

 

예제 실행 화면

 

ex)

//string
const char = 'c';
const brendan = 'brendan';
const greeting = 'hello ' + brendan;
console.log(`value: ${greeting}, type: ${typeof greeting}`);
const helloBob = `hi ${brendan}!`; // template literals(string)
console.log(`value: ${helloBob}, type: ${typeof helloBob}`); 

 

예제 실행 화면

 

ex)

//boolean
//false: 0, null, undefined, NaN, ''
//true: any other value
const canRead = true;
const test = 3 < 1; // false
console.log(`value: ${canRead}, type: ${typeof canRead}`);
console.log(`value: ${test}, type: ${typeof test}`);

 

예제 실행 화면

 

ex)

//null
let nothing = null;
console.log(`value: ${nothing}, type: ${typeof nothing}`);

//undefined
let x;
console.log(`value: ${x}, type: ${typeof x}`);

 

예제 실행 화면

 

ex)

// symbol, create unique identifiers for objects
const symbol1 = Symbol('id');
const symbol2 = Symbol('id');
console.log(symbol1 === symbol2);
const gSymbol1 = Symbol.for('id');
const gSymbol2 = Symbol.for('id');
console.log(gSymbol1 === gSymbol2);

 

예제 실행 화면

 

심볼은 바로 출력하면 에러가 나며 symbol.description과 같이 데이터 타입을 변환시켜 출력해야 한다.

 

ex)

//5. Dynamic typing: dynamically typed language
let text = 'hello';
console.log(`value: ${text}, type: ${typeof text}`);
text = 1;
console.log(`value: ${text}, type: ${typeof text}`);
text = '7' + 5;
console.log(`value: ${text}, type: ${typeof text}`);
text = '8' / '2';
console.log(`value: ${text}, type: ${typeof text}`);

text = 'hello'
console.log(text.charAt(0));
text = 4;
console.log(text.charAt(0)); // 에러 --> 타입스크립트가 나옴

 

예제 실행 화면

 

ex)

// object, real-lift object, data structure
const han = {name: 'han', age: 27};
han.age = 26;

 

han이라는 object는 잠겨서 값을 변경할 수 없지만 name과 age는 변경이 가능하다.

COMMENT