[JavaScirpt - 문법] 변수(Variable) (2)

테크야끼·2021년 4월 9일
0

JavaScript

목록 보기
6/13

7. 데이터 타입의 분류

1. primitive types (기본형)

  • primitive type은 더이상 작은 단위로 나누어질 수 없는 데이터를 뜻한다. primitive type은 변수에 할당될 때 메모리 상에 값(value) 자체가 저장되고 해당 변수가 원시 데이터 값을 보관한다.

  • primitive typeImmutable data types이다

    • number
    • string
    • boolean
    • null
    • undefined
    • symbol

2. Object types (참조형)

  • object type은 데이터 값을 묶어서 관리해주며, 각각의 크기가 정해져 있지 않고 저장된 데이터를 가르키고 있는 곳(reference)만 저장된다. object type은 참조 타입 데이터의 주소이며 해당 데이터의 값이 아니다.

  • 기본적으로 모든 objectmutable data types이다.
    (i.e. object.freeze())

    • array
    • function

first-class function?

JavaScript에서는 함수도 데이터 타입중에 하나이다. first-class function이 지원이 된다는 것은 함수도 변수에 할당이 가능하고 함수의 인자로 전달되며 return 타입으로도 return 할 수 있다는 뜻이다.

8. 데이터 타입

1. Number

JavaScript에서는 number의 데이터 크기를 지정하거나, 데이터타입을 선언할 필요가 없이 변수에 숫자를 넣으면 number type으로 할당이 되는 특성을 가지고있다.

const count = 17; //interger
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; //정수를 0으로 나눴을 때
const negativeInfinity = -1 / 0; //음수를 0으로 나눴을 때
const nAn = 'not a number' / 2; //숫자가 아닐 때
console.log(infinity); //infinity
console.log(negativeInfinity); //-Infinity
console.log(nAn); // NaN

2. 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}!`; //value: hi brendan, type:string
console.log(`value: ${helloBob}, type: ${typeof helloBob}`);
console.log('value: ' + helloBob + 'type: ' + typeof helloBob);

template literals
백틱기호(`)와 ${} 를 이용하면 변수의 값이 string과 붙어 출력된다.
줄바꿈을 하거나 싱글쿼트(')를 이용해도 그대로 출력되니 자주 사용하자!🤓

3.Boolean

  • false
    • 0
    • null
    • undefined
    • NaN
    • ''
  • true
    • false을 제외한 값

const canRead = true;
const test = 3 < 1; // fasle
console.log(`value: ${canRead}, type: ${typeof canRead}`); //value: true, type:boolean
console.log(`value: ${test}, type: ${typeof test}`);  //value: false, type:boolean

3. null , undefined

//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 

4. symbol

자료구조에서 고유한 식별자가 필요하거나 동시다발적으로 작동하는 코드에서 우선순위를 주고싶을 때 사용한다.

const symbol1 = Symbol('id');
const symbol2 = Symbol('id');
console.log(symbol1 === symbol2); //false
//동일한 id를 이용하여 symbol을 만들었지만 symbol1과 symbol2는 같지 않다.

const gSymbol1 = Symbol.for('id'); //동일한 symbol을 만들 때
const gSymbol2 = Symbol.for('id');
console.log(gSymbol1 === gSymbol2); //true
console.log(`value: ${symbol1.description}, type: ${typeof symbol1}`); //value: id, type: symbol

symbol을 출력할때 .description을 이용해 string으로 변환한 후 출력한다

9. Dynamic typing

JavcaScript는 Dynamically typed language라고 불리는데 c나 JAVA같은 프로그래밍 언어와 다르게, 변수를 선언할 때 어떤 타입인지 선언하지 않아도 프로그램이 동작할때 할당된 값에 따라 타입이 변경된다.

let text = 'hello'; //변수에 string 할당
console.log(`value: ${text}, type: ${typeof text}`); //value: hello, type: string
text = 1; //number 재할당
console.log(`value: ${text}, type: ${typeof text}`); //value: 1, type: number
text = '7' + 5; //string + number
console.log(`value: ${text}, type: ${typeof text}`);
//value: 75, type: string
text = '8' / '2'; //string / string
console.log(`value: ${text}, type: ${typeof text}`);
//value: 4, type: number

[참고]
https://www.youtube.com/watch?v=OCCpGh4ujb8
[이미지출처]
https://www.tutsmake.com/javascript-data-types/

0개의 댓글