JS — operator precedence
2 min readAug 21, 2018
연산자 우선권.
- precedence — 우선, priority
#Boolean
- Boolean(undefined) // false
- null, “ ” , 0 // false
- Boolean(2) // true
- string , 1,
- (a) — converto a Boolean
var a;
if(a){console.log('it's true)}
//output
// nothing will output.
//a is undefined.
- this code fail.
var a;a = 0; //falseif(a || a === 0){ true 일때 block 실행
console.log('something is there.')
}
//something is there
- run ..||.. then run ===
- boolean(0) // false
- a===0 is true
- false || true — return true
- either one is true or both are true this function returns true.
- 둘중에 하나가 true 이거나 둘다 true 이면 true 를 반환한다.
undefined || ‘hello’
//hellonull || 'hello
//hello"" || 'hello'
//hello
true 인 것을 반환. string 반환// name || '<your name here>'
if name is true , return name
or if it's not, return 'your name here'
둘중에 true 인것을 반환.
//절대 true , false를 반환하는게 아니다. true 일때 무엇을 반환할것인지를 결정.
#Number
- Number(undefined) // 0
- Number(null) // 0
3 == 3 // true
'3' == 3 //true
'3' === 3 //false
#multiplication has precedence (14), Unary plus is 16
console.log(3 + 4 * 5); // 3 + 20
// expected output: 23
javascript engine compute as human does because the operator precedence.
#Assignment … = …
- precedence — 3
- Associativity — right-to-left
- b=5
- a = b
- a = 5
var a;
var b;console.log(a = b = 5);
// expected output: 5;
논리 부정 연산자. iscustom = undefinedif( !iscustom ) { //ture - iscustom이 undefined 가 아니면.
console.log('is not custom!')
animOpts.complete = callback;
}
Logical NOT (!
)!expr
단일 피연산자가 true
로 변환될 수 있는 경우 false
를 반환합니다. 그렇지 않으면 true
를 반환합니다.
n1 = !true // !t returns false
n2 = !false // !f returns true
n3 = !"Cat" // !t returns false
거짓으로 변환할 수 있는 표현의 예는 다음과 같습니다.
null
;NaN;
0
;- empty string (
""
); undefined
.
# reference
Expressions and operators
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
#words
- compute 계산하다.