on
[JavaScript] 드림코딩 -자바스크립트 3
[JavaScript] 드림코딩 -자바스크립트 3
https://www.youtube.com/watch?v=OCCpGh4ujb8
1. Use strict (added in ES5)
"use strict";
- 자바스크립트는 굉장히 유연한 언어다.
- 선언되지 않은 값을 할당하거나 기존에 존재하는 프로토타입을 변경하는 것 등이 가능하다.
- "use strict";를 선언하면 strict 모드가 활성화되고 이러한 것들을 방지하여 안전한 코딩을 할 수 있다.
- "use strict";는 최상단에 있어야 한다.
2. Variable = 변수 (added in ES6)
let
let globalName = "global name"; { let name = "inin"; console.log(name); // inin name = "hello"; console.log(name); // hello console.log(globalName); // global name } console.log(name); // 블록 안에 있어서 접근 못 함 console.log(globalName); // global name
var
- 쓰지 말기 ---------- let, const로 대체
- var로 선언한 변수는 선언 전에 사용해도 에러가 나지 않는다.
- var는 이미 선언되어 있는 이름과 같은 이름으로 변수를 선언해도 에러가 나지 않는다.
- var로 선언한 변수는 함수 내부에서만 지역변수로 취급되고, 나머지는 모두 전역변수로 간주한다. (블록을 무시한다.)
- var를 사용하면 뒤에서 선언된 변수가 앞에서 참조되어도 에러가 나지 않는다.
(호이스팅 : 선언된 변수 및 함수를 코드의 최상단으로 끌어올리는 것)
3. Constants = 상수 (added in ES6)
const
- 한번 할당한 값을 절대 변경할 수 없다.
- 반드시 초기값을 할당해야 한다.
4. Variable types
- primitive data type : number, string, boolean, null, undefined, symbol
- object/reference type : object
number
const count = 17; // integer const size = 17.1; // decimal number console.log(`value: ${count}, type: ${typeof count}`); // value: 17, type: number console.log(`value: ${size}, type: ${typeof size}`); // value: 17.1, type: number
- 자바스크립트는 하나의 숫자 타입만 존재한다. ---------- number
const infinity = 1 / 0; const negativeInfinity = -1 / 0; const nAn = "not a number" / 2; console.log(infinity); // Infinity console.log(negativeInfinity); // -Infinity console.log(nAn); // NaN
- Infinity : 양수를 0으로 나눌 때 (양의 무한대)
- -Infinity : 음수를 0으로 나눌 때 (음의 무한대)
- NaN : 숫자가 아닌 값을 나눌 때 (산술 연산 불가, Not-a-Number)
bigInt
const bigInt = 123456789012345678901234567890n; // over (-2*53) ~ 2*53 console.log(`value: ${bigInt}, type: ${typeof bigInt}`); // value: 123456789012345678901234567890, type: bigint
- 정수 끝에 n을 붙이거나 함수 BigInt를 호출하면 BigInt 타입의 값을 만들 수 있다.
string
const char = "c"; const brendan = "brendan"; const greeting = "hello " + brendan; console.log(`value: ${greeting}, type: ${typeof greeting}`); // value: hello brendan, type: string const helloBob = `hi ${brendan}!`; // template literals(string) console.log(`value: ${helloBob}, type: ${typeof helloBob}`); // value: hi brendan!, type: string console.log("value: " + helloBob + " type: " + typeof helloBob); // value: hi brendan! type: string
boolean
const canRead = true; const test = 3 < 1; console.log(`value: ${canRead}, type: ${typeof canRead}`); // value: true, type: boolean console.log(`value: ${test}, type: ${typeof test}`); // value: false, type: boolean
- true : any other value
- false : 0, null, undefined, NaN, ''
null
let nothing = null; console.log(`value: ${nothing}, type: ${typeof nothing}`); // value: null, type: object
- 의도적으로 변수에 값이 없다는 것을 명시할 때 사용한다.
undefined
let x; console.log(`value: ${x}, type: ${typeof x}`); // value: undefined, type: undefined
- 선언은 되었지만 값을 할당하지 않은 변수는 undefined 값을 가진다.
symbol (added in ES6)
const symbol1 = Symbol("id"); const symbol2 = Symbol("id"); console.log(symbol1 === symbol2); // false const gSymbol1 = Symbol.for("id"); const gSymbol2 = Symbol.for("id"); console.log(gSymbol1 === gSymbol2); // true console.log( `value: ${symbol1.description}, type: ${typeof symbol1.description}` // value: id, type: string );
- 변경 불가능한 원시 타입의 값이다.
- 주로 이름의 충돌 위험이 없는 유일한 객체의 프로퍼티 키를 만들기 위해 사용한다.
object
const ellie = { name: "ellie", age: 20 }; ellie.age = 21;
- ellie는 const로 정의되어서 값을 변경 못하지만 ellie.name, ellie.age는 변경 가능하다.
5. Dynamic typing
let text = "hello"; console.log(text.charAt(0)); // h console.log(`value: ${text}, type: ${typeof text}`); // value: hello, type: string text = 1; console.log(`value: ${text}, type: ${typeof text}`); // value: 1, type: number text = "7" + 5; console.log(`value: ${text}, type: ${typeof text}`); // value: 75, type: string text = "8" / "2"; console.log(`value: ${text}, type: ${typeof text}`); // value: 4, type: number
- "text"는 string 타입이다.
- "text"에 숫자 1을 할당하면 데이터 타입이 number로 변경된다.
- "text"에 string 7과 number 5를 더한 값 할당 ---------- "text"가 string 타입으로 변환된다.
- "text"에 string 8과 string 2을 나눴다. ---------- "text"가 number 타입으로 변환된다.
from http://fried-rice.tistory.com/32 by ccl(A) rewrite - 2021-10-08 02:01:39