JavaScript 스터디(2)

Hvvany·2022년 12월 3일
0

Javascript

목록 보기
4/12

자료형

숫자형

숫자형(number type)정수부동소수점 숫자(floating point number)를 나타낸다.

let n = 123;
n = 12.345;

Infinity, -Infinity, NaN같은 '특수 숫자 값(special numeric value)'이 포함된다.

  • Infinity : 무한대
  • NaN : not a number(에러)
alert( 1 / 0 ); // 무한대

alert( "숫자가 아님" / 2 ); // NaN, 문자열을 숫자로 나누면 오류가 발생합니다.

문자형

자바스크립트에선 문자열(string)을 따옴표로 묶는다.

let str = "Hello";
let str2 = 'Single quotes are ok too';
let phrase = `can embed another ${str}`;
  • ' " : 큰 따옴표 작은 따옴표 같음. 따옴표 안에 따옴표 구분할려면 함께 사용
  • 백틱 ` : 역 따옴표라고 하는데 ${…} 를 사용할때 감싸준다.
let name = "John";

// 변수를 문자열 중간에 삽입
alert( `Hello, ${name}!` ); // Hello, John!

// 표현식을 문자열 중간에 삽입
alert( `the result is ${1 + 2}` ); // the result is 3

// 큰따옴표는 확장 기능을 지원하지 않습니다.
alert( "the result is ${1 + 2}" ); // the result is ${1 + 2} 

불린형

true, false

let nameFieldChecked = true; // 네, name field가 확인되었습니다(checked).
let ageFieldChecked = false; // 아니요, age field를 확인하지 않았습니다(not checked)
let isGreater = 4 > 1;

alert( isGreater ); // true (비교 결과: "yes")

null

어떠한 자료형도 아님.
존재하지 않는(nothing) 값, 비어 있는(empty) 값, 알 수 없는(unknown)

let age = null;

undefined

undefined 값도 null 값처럼 자신만의 자료형을 형성
값이 할당되지 않은 상태

let age;

alert(age); // 'undefined'가 출력됩니다.

객체와 심볼

객체(object)형은 특수한 자료형
다른 자료형은 한 가지만 표현할 수 있기 때문에 원시(primitive) 자료형
반면 객체는 데이터 컬렉션이나 복잡한 개체(entity)를 표현가능

심볼(symbol)형은 객체의 고유한 식별자(unique identifier)를 만들 때 사용

typeof 연산자

typeof 연산자는 인수의 자료형을 반환

typeof undefined // "undefined"

typeof 0 // "number"

typeof 10n // "bigint"

typeof true // "boolean"

typeof "foo" // "string"

typeof Symbol("id") // "symbol"

typeof Math // "object"  (1)

typeof null // "object"  (2)

typeof alert // "function"  (3)
profile
Just Do It

0개의 댓글