Deep Dive 07장 연산자

@hanminss·2021년 11월 15일
0

Deep Dive

목록 보기
4/16
post-thumbnail

모던 자바스크립트 deep dive

study with : zigum man

07장 연산자

1. 산술 연산자

  • 산술 연산이 불가능한 경우, return NaN(Not a Number)
  • 증감연산자(++/--)는 암묵적 할당이다.
  • 전위 증감 연산자 : ++x
  • 후위 증감 연산자 : x--
  • 숫자타입이 아닌 피연산자에 +,- 를 사용하면 숫자타입으로 변환된다.
  • 문자열 + 숫자, 불리언 + 숫자 등은 암묵적 타입변환이 일어난다.
var x = "1";

console.log(+x) // 1
console.log(x) // "1"

x = true;
console.log(+x) // 1

x ="hello";
console.log(+x) // NaN

'1' + 2; // '12'
1 + true; // 2

2. 비교 연산자

  • == , === // 동등비교, 일치비교
  • !=. !== // 비동등비교, 불일치 비교
  • isNaN() : NaN = true , not NaN = false
  • NaN과 NaN을 일치비교하면 false가 나온다. 따라서 둘을 비교할때는 Object.is() 메서드를 써야한다.
NaN===NaN // false
object.is(NaN,NaN) // true

3. 삼항 조건 연산자

  • 조건식의 평가 결과에 따라 반환할 값을 결정한다.
  • if-else 문처럼 쓸 수 있지만 큰 차이는 if-else문은 값으로 쓸 수 없다는 것이다.
var result = rank > 55 ? : 'good':'fail';

4. typeof 연산자

  • null을 typeof하면 null이 아닌 object가 나온다. 이는 js의 첫 버전의 오류다. 이는 앞으로도 고쳐지지 않을것이다.

5. 지수 연산자

  • 거듭제곱 : 2**2, Math.pow(2,2);
  • 음수의 거듭제곱은 괄호로 묶어야한다.
2**2; // 4
Math.pow(2,2); // 4
(-2)**3 // -8
x**=3;

6. 연산자의 부수효과

  • 대개 연산자든 다른 코드에 영향을 주지 않지만 몇몇 연산자들은 다른 코드에 영향을 주는 부수효과가 있다.
  • =, ++, --, delete
  • 할당연산자는 변수의 값이 변하는 부수효과
  • 증감연산자는 피연산자의 값을 바꾸는 부수효과
  • delete는 객체 프로퍼티를 삭제하는 부수효과

JS interview

9. What is the difference between == and === operators

출처

JavaScript provides both strict(===, !==) and type-converting(==, !=) equality comparison.
자바스크립트는 엄격하게 비교하는 일치비교(===,!==)와 값만 보는 동등비교(==,!=) 두가지를 제공한다.

The strict operators take type of variable in consideration, while non-strict operators make type correction/conversion based upon values of variables.
엄격한 연산자는 변수의 타입까지 고려하고, 엄격하지 않은 연산자는 변수의 값을 기초로 타입을 수정/변환 하여 비교한다.

The strict operators follow the below conditions for different types,
엄격한 연산자는 다른 타입을 아래의 조건에 따라 처리한다.

Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
두 문자열은 같은 문자, 길이, 각 문자의 위치를 엄격하게 비교한다.
Two numbers are strictly equal when they are numerically equal. i.e, Having the same number value.
두 숫자는 수학적으로 같은지 엄격하게 비교한다.(같은 값의 숫자인지)
There are two special cases in this,
NaN is not equal to anything, including NaN.

두개의 특별한 규칙이 있다. NaN은 자신을 포함하여 어떤것과도 같지 않다.
Positive and negative zeros are equal to one another.
-0과 +0은 같다.
Two Boolean operands are strictly equal if both are true or both are false.
불리안 형식은 둘다 true이거나 둘다 false인지 엄격하게 비교한다.
Two objects are strictly equal if they refer to the same Object.
두 객체는 같은 객체를 참조하는지 엄격하게 확인한다.
Null and Undefined types are not equal with ===, but equal with ==. i.e, null===undefined --> false but null==undefined --> true
null과 undefined의 타입은 == 에서는 같고 ===에서는 같지 않다.
Some of the example which covers the above cases,

0 == false   // true
0 === false  // false
1 == "1"     // true
1 === "1"    // false
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false
[]==[] or []===[] //false, refer different objects in memory
{}=={} or {}==={} //false, refer different objects in memory

0개의 댓글