자바스크립트 완벽 가이트 6판은 ECAMScript5와 HTML5를 기준으로 다루고 있음.
값 | String | Number | Boolean | Object |
---|---|---|---|---|
undefined | "undefined" | NaN | false | TypeError |
null | "null" | 0 | false | TypeError |
true | "true" | 1 | new Boolean(true) | |
false | "false" | 0 | new Boolean(false) | |
"" | 0 | false | new String("") | |
"1" | 1 | true | new String("1") | |
"a" | NaN | true | new String("a") | |
0 | "0" | false | new Number(0) | |
NaN | "NaN" | false | new Number(NaN) | |
Infinity | "Infinity" | true | new Number(Infinity) | |
1 | "1" | true | new Number(1) | |
[] | "" | 0 | true | |
[1] | "1" | 1 | true | |
['a'] | "a" | NaN | true | |
function(){} | "function(){}" | NaN | true |
: 인터프리터가 값으로 평가하는 JavaScript 구문
1
"hello"
/pattern/
...
예약어 : ture
false
null
this
변수 : i
student
undefined
배열 : []
[1,2,3]
[1,,2]
[[1,2], [3,4]]
...
객체 : {}
{ x:0, y:1 }
{ ax: { x:1, y:2 }, ay: { x:3, y:4 } }
...
a.x
a[x]
a[1]
...var f = function(a) { return a; }
... f(0)
Math.random()
...new Object()
new Object
new Point(1,3)
... 1 + 2 // => 3
1 + "2" // => "12"
"1" + 2 // => "12"
"1" + "2" // => "12"
1 + null // => 1
1 + undefined // => NaN
단항 산술 연산자
: + - ++ --
비트 연산자
: & | ^ ~
AND OR XOR NOT
: << >>>
Zero Fill - LeftShift RightShift
: >>
RightShift ( 가장 왼쪽 비트 보존, 가장 우측으로 )
==
~와 동등 (equal)
1 == '1' // => true, 1 === Number("1") 로 비교함.
1 == true // => true
'1' == true // => true
0 == -0 // => true
null == null // => true
undefined == undefined // => true
null == undefined // => true
===
~와 일치 (identical)
두 값이 모두 null
undefined
true
false
면 일치
두 값이 모두 같은 객체
배열
함수
를 참조하면 일치
두 값이 서로 다른 객체
를 참조하면 프로퍼티가 같아도 불일치
하나의 값이라도 NaN
이면 불일치 ( n !== n
이 true
인 유일한 값)
두 값의 타입이 다르면 불일치, 0
과 -0
은 일치
1 === '1' // => false
1 === true // => false
'1' === true // => false
0 === -0 // => true
null === null // => true
undefined === undefined // => true
null === undefined // => false
==
대신에 ===
를 사용하자 )Array(3) == ",," // => true
[0] == [0] // => false, new String("0") == new String("0")
[] == ![] // => true
<
>
<=
>=
숫자 or 문자열만 비교
'a' > 1 // => false
'a' < 1 // => false
1 > 'a' // => false
1 < 'a' // => false
'a' > '1' // => true
'a' < '1' // => false
1 < 9 // => true
'1' < 9 // => true
1 < '9' // => true
피연산자 중 NaN
=> 무조건 false
Infinity
는 Infinity
제외한 어떤 수보다 큼
-Infinity
는 -Infinity
제외한 어떤 수보다 작음
숫자 or 문자열이 아닌 피연산자는 먼저 변환
문자열 비교 String.localCompare()
대문자 변환 String.toUpperCase()
소문자 변환 String.toUpperCase()
var obj = {a:1, b:2};
"a" in obj; // => true
"z" in obj; // => false
"valueOf" in obj // => true, 상속된 프로퍼티 true
var arr = [1,2,3];
"0" in arr; // => true, 0 번째 원소
1 in arr; // => true
5 in arr; // => false
var num = new Number();
num instanceof Number; // => true
num instanceof Object; // => true, 모든 객체는 Object의 인스턴스
var arr = [1,2,3];
arr instanceof Array; // => true
arr instanceof Object; // => true
arr instanceof Number; // => false
var a = "a";
a instanceof Object; // => false, 객체가 아니면 false
&&
||
!
: 피연산자 값을 반전하기 전에 불리언 값으로 변환a = b
a.x = b
a += b
a =- b
a *= b
a /= b
a %= b
a <<= b
a >>= b
a >>>= b
a &= b
a |= b
a ^ b