const integer = 30; // 정수
const double = 30.12l // 실수
const negative = -30; // 음의 정수
const binary = 0b01000001; // 2진수
const octal = 0o101; // 8진수
const hex = 0x41; // 16진수
// 자바스크립트는 위의 진수를 표현하기 위한 데이터 타입X, 전부 10진수로 해석
console.log(binary); // 65
console.log(octal); // 65
console.log(hex); // 65
console.log(binary === octal); // true
console.log(octal === hex); // true
console.log(3 === 3.0); // true
console.log(4 / 2); // 2
console.log(3 / 2); // 1.5
// 추가적으로 3가지 특별한 값도 표현 가능
console.log(10 / 0); // Infinity (양의 무한대)
console.log(10 / -0); // -Infinity (음의 무한대)
console.log(1 * 'String'); // NaN (산술 연산 불가)
const x = nan; // ReferenceError: nan is not defined
const String;
String = '문자열'; // 작은따옴표
String = "문자열"; // 큰따옴표
String = `문자열`; // 백틱(ES6)
String = '작은따옴표로 감싼 문자열 내의 "큰따옴표"는 문자열로 인식'; // 반대도 동일
const String = hello; // ReferenceError: hello is not defined
const temlate = `Template literal`;
console.log(tempalte); // Template literal
const str = `Hello
world.`; // SyntaxError: Invalid or unexpected token
// 입력
const template = '<ul>\n\t<li><a href="#">HOME</a></li>\n</ul>';
console.log(template);
// 출력
<ul>
<li><a href="#">HOME</a></li>
</ul>
// 입력
const template = `<ul>
<li><a href="#">HOME</a></li>
</ul>`;
console.log(template);
// 출력
<ul>
<li><a href="#">HOME</a></li>
</ul>
// ES5: 문자열 연결
const first = 'Ung-mo';
const last = 'Lee';
consolo.log('My name is' + first + ' ' + last + '.'); // My name is Ung-mo Lee.
// ES6: 표현식 삽입
const first = 'Ung-mo';
const last = 'Lee';
console.log(`My name is ${first} ${last}.`); // My name is Ung-mo Lee.
console.log(`1 + 2 = ${1 + 2}`); // 1 + 2 = 3
console.log('1 + 2 = ${1 + 2}'); // 1 + 2 = ${1 + 2}
let foo = true;
console.log(foo); // true
foo = false;
console.log(foo); // flase
let foo = 'Lee';
// 이전에 참조를 제거
// 유용하지 않음, 변수의 스코프를 좁게 만들어 변수 자체를 재빨리 소멸시키는 편이 굿
foo = null;
<!DOCTYPE html>
<html>
<body>
<script>
const element = document.querySelector('.myClass');
// HTML 문서에 myClass 클래스를 갖는 요소가 없다면 null 반환
console.log(element); // null
</script>
</body>
</html>
let foo;
console.log(foo); // undefined
// 심벌 값 생성
const key = Symbol('key');
console.log(typeof key); // symbol
// 객체 생성
const obj = {};
// 이름이 충돌할 위험이 없는 유일무이한 값인 심벌을 프로퍼티 키로 사용
obj[key] = 'value';
console.log(obj[key]); // value
const theBiggestInt = 9007199254740991n;
console.log(theBiggestInt); // 9007199254740991n
const alsoHuge = BigInt(9007199254740991);
console.log(alsoHuge); // 9007199254740991n
const hugeString = BigInt("9007199254740991");
console.log(hugeString); // 9007199254740991n
const hugeHex = BigInt("0x1fffffffffffff");
console.log(hugeHex); // 9007199254740991n
const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111");
console.log(hugeBin); // 9007199254740991n
const person = {
name: 'kim',
age: 30,
address: {
city: 'seoul',
zip: '12345'
}
};
console.log(person.name); // kim
console.log(person['age']); // 30
console.log(person.address.city); // seoul
// 배열 생성 (빈 배열)
const arr = [];
arr[0] = 'zero';
arr[1] = 'one';
arr[2] = 'two';
arr[3] = 'three';
console.log(arr[0]); // zero
console.log(arr.length) // 4
console.log(typeof arr) // object
// 배열 생성 (초기 값 할당)
const arr = [
'zero', 'one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine'
];
console.log(arr[0]); // zero
console.log(arr.length) // 10
console.log(typeof arr) // object
const obj = {
'0': 'zero', '1': 'one', '2': 'two',
'3': 'three', '4': 'four', '5': 'five',
'6': 'six', '7': 'seven', '8': 'eight',
'9': 'nine'
};
const misc = [
'string',
10,
true,
null,
undefined,
NaN,
Infinity,
['nested array'],
{ object: true },
function () {}
];
console.log(misc.length); // 10
const aaa = [];
console.log(typeof aaa); // 배열형 -> object 출력
const bbb = {};
console.log(typeof bbb); // 객체형 -> object 출력
const ccc = function () {};
console.log(typeof ccc); // 함수형 -> function 출력
// 기명 함수 표현식(named function expression)
const foo = function multiply(a, b) {
return a * b;
};
// 익명 함수 표현식(anonymous function expression)
const bar = function(a, b) {
return a * b;
};
console.log(foo(10, 5)); // 50
console.log(bar(10, 5)); // 50
console.log(multiply(10, 5)); // Uncaught ReferenceError: multiply is not defined
const foo = function (a, b) {
return a * b;
};
const bar = foo;
console.log(foo(10, 10)); // 100
console.log(bar(10, 10)); // 100
=> 사용할 때 다른 요소들과 아무런 차별이 없다는 것을 의미
const yolo = function() {
console.log("yolo yolo");
}
const yolo = function() {
console.log("yolo yolo");
}
function print(func) {
func();
}
print(yolo);
const yolo = function() {
console.log("yolo yolo");
return function() {
console.log("yolo yolo yolo");
}
}
const yolo2 = yolo();
yolo2();