코드를 작성할 때 중요한 점
1. 데이터 전송
2. CPU에 최적화된 연산
3. 메모리의 사용을 최소화
rw (read/write) 값을 읽고 쓰는 것이 가능
1. ES6에 추가되어있음
2. ES6 이전 문법은 잘 사용하지 않음
3. 변수를 선언할 수 있는 명령어 딱하나 let
4. 만약 누군가 var를 쓰고 있다면,,? 쓰지말라 하십셔
let comment = 'ellie'
console.log(comment); // ellie
comment = 'hello';
console.log(comment) //hello
block scope
- 전역변수와 지역변수
- block scope를 정의하지 않으면 어디서든지 사용이 가능하다
- block scope안에 정의한 것은 안에서만 사용할 수 있음
- 밖에서 block scope안의 변수를 console로 찍으면 아무것도 안나오는 것을 확인할 수 있음
- 글로벌한 변수는 application이 실행되는 순간부터 끝날 때까지 메모리에 탑재되어있기 때문에 최소한으로 쓰는 것이 좋음
- class나 함수 if 나 for 등의 필요한 부분에서만 정의해서 쓰는 것이 좋음
let globalName = 'global name';
{
let name = 'ellie'
console.log(name); //ellie
name = 'hello';
console.log(name) //hello
console.log(globalName) // global name
}
console.log(name); // undefined
console.log(globalName); //global name
쓰지말아야할 변수 할당 키워드
var hoisting(끌어올려줌)발생
var hoisting이란?
어디에 선언했는가에 상관없이 항상 제일 위로 선언을 끌어 올려주는 것
block scope이 없음 ⇒ block scope를 철저히 무시한다
console.log(age) // undefined가 찍힘
age = 4;
var age; // file맨 위에 선언이 올라가게 됨
console.log(age) // 4
//block scope
{
age = 4;
var age;
}
console.log(age) //맨위에 undefined가 찍힘
- 그래서 나온 것이 let!
r(read only)만 가능해서 값 변경 불가능
평소에 constant로 작성하는 것이 바람직
한번 할당하면 값이 절대 바뀌지 않는 것
const daysInWeek = 7;
const maxNumber = 5;
어떤 프로그래밍 언어든 primitive type와 object type으로 나뉨
primitive type: 더이상 작은 단위로 나누어 질 수 없는 한가지 아이템
single item: Number, string, boolean, null, undefined, symbol
Object type: primitive type을 모아 박스로 관리
box container
function type: js에서는 function도 데이터 타입처럼 변수에 할당 가능하고 함수의 parameter 인자 전달도 가능하고 return type으로도 return가능
first-class function
//C언어 자료형
int main() {
short a = 12 //2 bytes
int a = 12; //4 bytes
long b = 1234; //8bytes
float d = 1.2f // 4bytes
double e = 8.2; //16 bytes
return 0
}
number면 모든 것이 해결
게다가 type이 다이나믹하게 정의되기 때문에 number라고 선언하지 않아도 됨
let count = 12;
let size = 1.2;
console.log(`value: ${count}, type: ${typeof count}`)
// value: 12, type: number
console.log(`value: ${size}, type: ${typeof size}`)
// value: 1.2 type: number
연산이 정말 valid한 연산인지 확인하는 것이 중요
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
over (-253 ~ 253) 일 경우에도 수가 나오게 해줌
const bigInt = 1234567890123456789012345678901234567890n; // over (-2**53) ~ 2*53)
console.log(`value: ${bigInt}, type: ${typeof bigInt}`);
//value: 1234567890123456789012345678901234567890, type: 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
// 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}`);
// null
let nothing = null;
console.log(`value: ${nothing}, type: ${typeof nothing}`);
// undefined
let x;
console.log(`value: ${x}, type: ${typeof x}`);
변수.description
필수 // symbol, create unique identifiers for objects
const symbol1 = Symbol('id');
const symbol2 = Symbol('id');
console.log(symbol1 === symbol2); //false
//동일한 symbol을 만들고 싶을 때 symbol.for
const gSymbol1 = Symbol.for('id');
const gSymbol2 = Symbol.for('id');
console.log(gSymbol1 === gSymbol2); // true
console.log(`value: ${symbol1.description}, type: ${typeof symbol1}`);
박스 형태로 변수 지정
.
을 통해 변경가능함 // object, real-life object, data structure
const ellie = { name: 'ellie', age: 20 };
ellie.age = 21; // 변경가능
js는 dynamically type language라고 불림
C나 Java는 static type language라고 부름
+
를 문자 합으로 이해해서 string으로 됨/
를 연산으로 이해해서 type이 num으로 바뀜Type Script
가 나오게 됨 // 5. Dynamic typing: dynamically typed language
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
//string + num = string
text = '7' + 5;
console.log(`value: ${text}, type: ${typeof text}`); //value: 75, type: string
//string / strnig = number
text = '8' / '2';
console.log(`value: ${text}, type: ${typeof text}`); //value: 4, type: number
console.log(text.charAt(0)); // time error