javascript
[javascript] 데이터타입
tonirr
2020. 10. 22. 00:03
```
// 1. 바닐라 javascript 로 개발할 떄에는 use strict로
// 선언해 줄 경우 선언되지 않은 변수에 대해 에러를 보여줌
'use strict';
// 2. Variable
// let (added in ES6)
let globalName = 'global name';
{
let name = 'soyeon';
console.log(name);
name = 'hello';
console.log(name);
}
console.log(globalName);
console.log(name);
// var (쓰지마)
// var hoisting (어디에 선언했는지에 상관없이 가장 제일위로 선언을 끌어올려줌)
// console.log(age);
// age = 4;
// console.log(age);
// var age;
{
age = 4;
console.log(age);
var age;
}
console.log(age);
// 3. Contants
// const
// - security
// - thread safety
// - reduce human mistakes
const daysInWeek = 7;
const maxNumber = 5;
// 4. Variable types
// primative, single item: number, string, boolean, null, undefined, symbol
// object, box container
// function, first-class function
const count = 17;
const size = 17.1;
console.log(`value: ${count}, type: ${typeof count}`);
console.log(`value: ${size}, type: ${typeof size}`);
// number - speicla numeric 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);
// string
const char = 'c';
const brendan = 'brendan';
const greeting = 'hello ' + brendan;
console.log(`value: ${greeting}, type: ${typeof greeting}`);
const helloBob = `hi ${brendan}!`;
console.log(`value: ${helloBob}, type: ${typeof helloBob}`);
console.log('value: '+helloBob+ ', type: '+ typeof helloBob);
// boolean
const canRead = true;
const test = 3 < 1;
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}`);
// symbol - 동일한 string이더라도 다름
const symbol1 = Symbol('id');
const symbol2 = Symbol('id');
console.log(symbol1 === symbol2);
console.log(`value: ${symbol1.description}, typeof: ${symbol2.description}`);
// 5. Dynamic typing: dynamically typed language
let text = 'hello';
console.log(text.charAt(0));
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}`);
console.log(text.charAt(0));
```