[Study/JavaScript] typeof / 연산자 우선 순위 / 형 변환

SoShy·2023년 11월 27일

JavaScript_Study

목록 보기
5/36
post-thumbnail

🥕 typeof 연산자

  • typeof 키워드 다음에 값을 입력하면, 이 값을 평가해서 해당하는 자료형을 문자열로 되돌려 줌.
console.log(typeof 101);
console.log(typeof 'SoShy');
console.log(typeof true);
.
.
.
>>> number
	string
    boolean
let name = 'SoShy';

function sayHello() {
  console.log('Hello');
};

console.log(typeof name);
console.log(typeof sayHello);
.
.
.
>>> string
	function

  • JS는 소수와 정수를 따로 구분하지 않기 때문에, 둘 다 number로 출력됨.
console.log(1);
console.log(1.0);
console.log('1');
console.log("1");
console.log(`1`);
.
.
.
>>> number
	number
    string
    string
    string

  • typeof 연산자는 사칙연산자보다 우선순위가 높음.
console.log(typeof 'Hello' + 'SoShy');
console.log(typeof 8 - 3);
.
.
.
>>> stringCodeit
	NaN		// not a number의 약자로, 숫자가 아닌 값을 표현하는 값
console.log(typeof ('Hello' + 'SoShy'));
console.log(typeof (8 - 3));
.
.
.
>>> string
	number

🥕 연산자 우선 순위

우선 순위연산자 유형기호
21그룹( )
...
17논리 NOT!
17typeoftypeof
16거듭제곱**
15곱셉*
15나눗셈/
15나머지%
14덧셈+
14뺄셈-
...
12미만<
12이하<=
12초과>
12이상>=
11동등==
11부등!=
11일치===
11불일치!==
...
6논리 AND&&
5논리 OR!!
...
3할당=
typeof (6 * 2 === 11 || 13 - 7 < 7 && !true);
.
.
.
>>> boolean

🥕 형 변환 (Type Conversion)

  • 처음에 값으로 정해진 자료형을 다른 자료형으로 바꾸는 것
  • 바꾸고자 하는 자료형을 Number(), String(), Boolean() 형태로 입력하면 됨.
console.log(Number('10') + Number('5'));
console.log(String(10) + String(5));
.
.
.
>>> 15
	105
let x = 123;

console.log(x);
console.log(String(x));
console.log(typeof x);
console.log(typeof String(x));
.
.
.
>>> 123
	123
	number
    string
let y = true;

console.log(y);
console.log(String(y));
console.log(typeof y);
console.log(typeof String(y));
.
.
.
>>> true
	true
	boolean
	string

  • boolean은 number로 형 변환할 때, 01로 값이 변함.
    - true -> 1
    - false -> 0
let y = true;

console.log(y);
console.log(Number(y));
console.log(typeof y);
console.log(typeof Number(y));
.
.
.
>>> true
	1
	boolean
    number
let z = false;

console.log(z);
console.log(Number(z));
.
.
.
>>> false
	0

  • 일반적으로 어떤 값을 boolean으로 변환할 때는, true 값이 됨.
let x = '문자';

console.log(x);
console.log(Boolean(x));
console.log(typeof x);
console.log(typeof Boolean(x));
.
.
.
>>> 문자
	true
	string
    boolean
let y = 123;

console.log(y);
console.log(Boolean(y));
console.log(typeof y);
console.log(typeof Boolean(y));
.
.
.
>>> 123
	true
	number
    boolean

  • 뭔가 없거나 비어 있는 듯한 느낌을 주는 값을 boolean으로 변환하면, false 값이 됨.
    - NaN도 boolean으로 변환하면, false 값이 됨.
let x = '';

console.log(x);
console.log(Boolean(x));
console.log(typeof x);
console.log(typeof Boolean(x));
.
.
.
>>> 
	false
	string
    boolean
let y = 0;

console.log(y);
console.log(Boolean(y));
console.log(typeof y);
console.log(typeof Boolean(y));
.
.
.
>>> 0
	false
	number
    boolean
let y = NaN;

console.log(y);
console.log(Boolean(y));
console.log(typeof y);
console.log(typeof Boolean(y));
.
.
.
>>> NaN
	false
	number
    boolean

1. Falsy

  • boolean 값으로 형변환 했을 때, false가 나오는 값
  • '', 0, NaN

2. 암시적 형 변환

  • 아래에서, 다른 자료형끼리의 연산임에도 불구하고 error가 나지 않는 이유는, 일정한 규칙에 따라 두 값이 자동으로 변환된 후, 연산이 이루어졌기 때문
console.log('4' - true);
.
.
.
>>> 3

1) 산술 연산자

  • +, -, *, /, %, **
  • 연산 되어지는 두 값을 모두 숫자형으로 변환한 다음 연산
  • +의 경우, 순서에 상관없이 어느 한쪽이라도 문자열이 있다면, 양쪽 모두를 문자열로 바꾼 다음, 문자열 연산으로 동작
console.log(4 + '2');
console.log(4 + 2);
console.log(4 - true);
console.log(4 * false);
console.log(4 / '2');
console.log('4' ** true);
console.log(4 % 'two');
.
.
.
>>> 42
	6
	3
	0
	2
	4
	NaN

2) 비교 연산자

  • <, <=, >, >=
  • 연산 되어지는 두 값을 모두 숫자형으로 변환한 다음 연산
  • 비교가 불가능한 경우, false 출력됨.
console.log(2 < '3');
console.log(2 > true);
console.log('2' <= false);
console.log('two' >= 1);
.
.
.
>>> true
	true
	false
	false

3) 같음 비교 연산

  • 일치 비교: ===, !==
    - 암시적 형변환이 일어나지 않음.

  • 동등 비교: ==, !=
    - 숫자 형태로 암시적 형변환 일어남.
console.log(1 === '1');
console.log(1 === true);
console.log(1 == '1');
console.log(1 == true);
.
.
.
>>> false
	false
	true
	true
console.log(5 % 2 === Boolean(2) * true);
.
.
.
>>> true
profile
프론트엔드 개발자가 되기 위해 노력 중인 새싹🌱 입니다.

0개의 댓글