티스토리 뷰
대부분의 프로그래밍 언어에서 데이터 타입을 확인할 수 있는 연산자를 제공한다. 자바스크립트도 데이터 타입을 확인할 수 있는 \(typeof\) 연산자를 제공한다. \(typeof\)연산자는 피 연산자의 데이터 타입을 문자열로 리턴한다. 사용법은 어렵지 않기때문에 실습 코드로 설명을 대신한다.
소스 코드
/* number example */ var intNum = 10 var floatNum = 3.14 console.log(typeof intNum, typeof floatNum) // number number /* string example */ var ch = 'a' var str = "hello world" console.log(typeof ch, typeof str) // string string /* boolean example */ var boolVal = true console.log(typeof boolVal) // boolean /* null, undefined example */ var nullVal = null var undefinedVal = undefined console.log(typeof nullVal) // object console.log(typeof undefinedVal) // undefined /* array, object example */ var arr = [1, 2, 3, 4] var obj = { name: 'kesakiyo' } console.log(typeof arr) // object console.log(typeof obj) // object /* function example */ var func = function(x, y) { return x + y } console.log(typeof func) // function
마무리
위 코드에서 유의해야 할 점이 몇 가지 있다. 첫 번째로 숫자는 정수형, 실수형 상관없이 \(number\) 타입을 가진다. 이는 자바스크립트에서 모든 숫자를 부동 소수점으로 저장하기 때문이다. 두 번째로 자바스크립트에서는 문자열과 \(char\) 구분이 없다. 세 번째로는 기본타입인 \(null\)이 \(object\) 타입을 가진다. 이 세가지는 헷갈리지 말고 정확히 기억하고 있어야 한다. 더 자세한 설명은 "자바스크립트의 데이터 타입"에서 읽을 수 있다.
'Javascript' 카테고리의 다른 글
자바스크립트의 데이터 타입 (0) | 2017.01.20 |
---|
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- graph
- brute force
- disjoint set
- bipartite matching
- Binary Search
- greedy
- Data Structure
- Sqrt Decomposition
- max flow
- DP
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
글 보관함