* ๊ธฐ๋ณธ ํ์ ๊ณผ ํ์ค ๋ฉ์๋
์๋ฐ์คํฌ๋ฆฝํธ๋ ๊ธฐ๋ณธ ํ์ (์ซ์, ๋ฌธ์์ด, ๋ถ๋ฆฐ๊ฐ)์ ๋ํด ๊ฐ๊ฐ ํธ์ถ ๊ฐ๋ฅํ ํ์ค ๋ฉ์๋๋ฅผ ์ ์ํ๊ณ ์๋ค. ๊ธฐ๋ณธ ํ์ ์ด ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฒฝ์ฐ์๋, ๋ฉ์๋ ์ฒ๋ฆฌ ์๊ฐ์ ๊ฐ์ฒด๋ก ๋ณํ๋ ํ ํ์ค ๋ฉ์๋๋ฅผ ํธ์ถํ๋ค. ๋ฉ์๋ ํธ์ถ์ด ๋๋๋ฉด ๋ค์ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ๋์์จ๋ค. ์ด๋ฌํ ๋ฐฉ์์ผ๋ก ๊ธฐ๋ณธ ํ์ ์ ๊ฐ์ฒด๊ฐ ์๋์๋ ๊ฐ ํ์ ๋ณ๋ก ํธ์ถ ๊ฐ๋ฅํ ํ์ค ๋ฉ์๋๋ค์ ๋ง์น ๊ฐ์ฒด์ฒ๋ผ ํธ์ถํ ์ ์๋ค.
var num = 0.5;
console.log(num.toExponential(1)); // 5.0e-1
console.log("stringtest".charAt(0)); // s
* ์ฐ์ฐ์
1. + ์ฐ์ฐ์
๋ํ๊ธฐ ์ฐ์ฐ, ๋ฌธ์์ด ์ฐ๊ฒฐ ์ฐ์ฐ ๋ ๊ฐ์ง ์ํ์ ํ๋ค. ์ซ์+์ซ์์ผ ๊ฒฝ์ฐ ๋ํ๊ธฐ ์ฐ์ฐ, ๋๋จธ์ง ๊ฒฝ์ฐ์๋ ๋ฌธ์์ด ์ฐ๊ฒฐ ์ฐ์ฐ์ ์ํํ๋ค.
var a = 1 + 4;
var b = 2 + 'bb';
var c = 'cc' + 3;
var d = 'dd' + 'ee';
console.log(a); // 5
console.log(b); // 2bb
console.log(c); // cc3
console.log(d); // ddee
1+4๋ฅผ ์ ์ธํ ๋ค๋ฅธ ์ฐ์ฐ์ ๋ฌธ์์ด ์ฐ๊ฒฐ ์ฐ์ฐ์ผ๋ก ์ํ๋์๋ค.
2. typeof ์ฐ์ฐ์
ํผ์ฐ์ฐ์์ ํ์ ์ ๋ฌธ์์ด ํํ๋ก ๋ฆฌํดํ๋ค.
var num = 1;
var str = 'ss';
var boo = true;
var n = null;
var ud = undefined;
var obj = new Object();
var arr = [];
var func = function a(){ };
console.log(typeof num); // ์ซ์ : number
console.log(typeof str); // ๋ฌธ์์ด : string
console.log(typeof boo); // ๋ถ๋ฆฐ๊ฐ : boolean
console.log(typeof n); // null : object
console.log(typeof ud); // undefined : undefined
console.log(typeof obj); // ๊ฐ์ฒด : object
console.log(typeof arr); // ๋ฐฐ์ด : object
console.log(typeof func); // ํจ์ : function
3. == (๋๋ฑ) ์ฐ์ฐ์, === (์ผ์น) ์ฐ์ฐ์
๋๋ฑ ์ฐ์ฐ์, ์ผ์น ์ฐ์ฐ์ ๋ชจ๋ ๋ ๊ฐ์ด ๋์ผํ์ง ํ์ธํ ๋ ์ฌ์ฉํ๋ค. == ์ฐ์ฐ์๋ ๋น๊ตํ๋ ค๋ ํผ์ฐ์ฐ์์ ํ์ ์ด ๋ค๋ฅผ ๊ฒฝ์ฐ ํ์ ๋ณํ์ ๊ฑฐ์น ํ ๋น๊ตํ๋ฉฐ, === ์ฐ์ฐ์๋ ํ์ ์ด ๋ค๋ฅผ ๊ฒฝ์ฐ์๋ ํ์ ๋ณํ์ ํ์ง ์๊ณ ๋น๊ตํ๋ค๋ ์ฐจ์ด์ ์ด ์๋ค.
console.log(1 == '1'); // true
console.log(1 === '1'); // false
1 == '1'์์ ํผ์ฐ์ฐ์ ๋์ ํ์ ์ด ๋ค๋ฅด๋ฏ๋ก, ๋ฌธ์์ด '1'์ ์ซ์ 1๋ก ๋ณํํด์ ๋น๊ตํ์๊ณ ๊ทธ ๊ฒฐ๊ณผ true๊ฐ ์ถ๋ ฅ๋์๋ค. 1 === '1'์ ๊ฒฝ์ฐ ๋์ ํ์ ์ด ๋ค๋ฅผ ๋๋ ํญ์ false๊ฐ ์ถ๋ ฅ๋๋ค.
== ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ฉด ํ์ ๋ณํ์ ๋ฐ๋ฅธ ์๋ชป๋ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ผ๋ฏ๋ก ๋๋ถ๋ถ์ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋ฉ ๊ฐ์ด๋์์๋ === ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํ๋ค. jQuery ์ฝ๋ฉ ๊ฐ์ด๋ ๋ผ์ธ์์๋ ๋ง์ฐฌ๊ฐ์ง๋ก === ์ฐ์ฐ์๋ฅผ ๊ถํ๊ณ ์๋ค.
4. !! ์ฐ์ฐ์
!! ์ฐ์ฐ์๋ ํผ์ฐ์ฐ์๋ฅผ ๋ถ๋ฆฐ๊ฐ์ผ๋ก ๋ณํํ๋ค.
console.log(!!0); // false
console.log(!!1); // true
console.log(!!'str'); // true
console.log(!!''); // false
console.log(!!true); // true
console.log(!!false); // false
console.log(!!null); // false
console.log(!!undefined); // false
console.log(!!{}); // true
console.log(!![]); // true
'๐ปStudy > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฐ์ด (0) | 2021.03.05 |
---|---|
ํ๋กํ ํ์ (0) | 2021.03.05 |
์๋ฐ์คํฌ๋ฆฝํธ์ ๋ฐ์ดํฐ ํ์ (0) | 2021.02.09 |
์๋ฐ์คํฌ๋ฆฝํธ์ ๊ฐ์์ ํต์ฌ ๊ฐ๋ (0) | 2021.02.09 |