B4 : μ›μ‹œν˜•μ˜ ν˜•λ³€ν™˜

2025. 8. 5. 19:43ㆍFront-end/JavaScript

λ°˜μ‘ν˜•

λͺ©μ°¨

1. ν˜•λ³€ν™˜

2. λ¬Έμžν˜•μœΌλ‘œ λ³€ν™˜

3. μˆ«μžν˜•μœΌλ‘œ λ³€ν™˜

4. λΆˆλ¦°ν˜•μœΌλ‘œ λ³€ν™˜

5. μ•”μ‹œμ  κ°•μ œ λ³€ν™˜

    5-1. λ§μ…ˆ μ—°μ‚°μž

    5-2. 동등 μ—°μ‚°μž

    5-3. 비ꡐ μ—°μ‚°μž

    5-4. 논리 μ—°μ‚°μž

    5-5. 쑰건 ν‘œν˜„μ‹


1. ν˜•λ³€ν™˜

μžλ°”μŠ€ν¬λ¦½νŠΈλŠ” κ΄€λŒ€ν•œ νƒ€μž… λ³€ν™˜μ„ μˆ˜ν–‰ν•˜μ—¬, μ–΄λ–€ κ²½μš°μ— 데이터 νƒ€μž… λ³€ν™˜μ΄ λ°œμƒν•˜λŠ” μ§€ μ•Œμ•„μ•Ό ν•œλ‹€.

μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ νƒ€μž… λ³€ν™˜μ€ 두 κ°€μ§€κ°€ μžˆλ‹€.

  • λͺ…μ‹œμ  κ°•μ œ λ³€ν™˜: κ°œλ°œμžμ˜ μ˜λ„적인 νƒ€μž… λ³€ν™˜
  • μ•”μ‹œμ  κ°•μ œ λ³€ν™˜: μ—°μ‚° 쀑 λ‚΄λΆ€μ μœΌλ‘œ νƒ€μž… λ³€ν™˜

각 νƒ€μž…(ν˜•)λ§ˆλ‹€μ˜ λͺ…μ‹œμ  κ°•μ œ λ³€ν™˜λ“€μ„ 보고 μ•”μ‹œμ  κ°•μ œ λ³€ν™˜μ€ λͺ°μ•„μ„œ 봐보자.


2. λ¬Έμžν˜•μœΌλ‘œ λ³€ν™˜

λ‹€λ₯Έ νƒ€μž…μ—μ„œ λ¬Έμžμ—΄ νƒ€μž…μœΌλ‘œμ˜ λ³€ν™˜μ€ ToString 좔상 μ—°μ‚° λͺ…μ„Έλ₯Ό κΈ°μ€€μœΌλ‘œ 이루어진닀.

λ¬Έμžμ—΄μ˜ λ³€ν™˜μ€ λŒ€λΆ€λΆ„ 예츑 κ°€λŠ₯ν•œ λ°©μ‹μœΌλ‘œ μΌμ–΄λ‚œλ‹€.

νƒ€μž… λ¬Έμžμ—΄ λ³€ν™˜ κ²°κ³Ό
undefined 'undefined'
null 'null'
boolean true β‡’ 'true', false β‡’ 'false'
number Number.toString()의 κ²°κ³Ό λ°˜ν™˜(3 β‡’ '3')
Symbol *TypeError
BigInt BigInt.toString()의 κ²°κ³Ό λ°˜ν™˜(3n  β‡’ '3n')
object 객체λ₯Ό μ›μ‹œ νƒ€μž…μœΌλ‘œ λ°˜ν™˜(ToPrimitive좔상연산)  β‡’ ToString 좔상 μ—°μ‚° μˆ˜ν–‰κ°’ λ°˜ν™˜

*Symbol이 λ³€ν™˜λ˜μ–΄ 객체의 λ‹€λ₯Έ ν”„λ‘œνΌν‹°μ— μ ‘κ·Όν•˜λŠ” 것을 λ°©μ§€ν•˜κ³ μž TypeError λ°œμƒ

 

String(), toString() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•΄μ„œ ν˜•λ³€ν™˜μ„ μΌμœΌν‚¨λ‹€.

// String()
console.log(String(false));	// 'false'
console.log(String(3));	// '3'

// toString()
const bool = false;
console.log(bool.toString());	// 'false'

const num = 4;
console.log(num.toString());	// '4'

 

String()κ³Ό toString()이 차이λ₯Ό λ³΄μ΄λŠ” 곳은 null, undefinedλ₯Ό λ³€ν™˜ν•  λ•Œμ΄λ‹€.

console.log(String(null))	// 'null'
console.log(String(undefined))	// 'undefined'

undefined.toString();	// TypeError
null.toString();	// TypeError

 

nullκ³Ό undefinedλŠ” μ›μ‹œ νƒ€μž…μ΄λ©°, κ°μ²΄κ°€ μ•„λ‹ˆκΈ° λ•Œλ¬Έμ—

κ°μ²΄μ—μ„œ μ‚¬μš©ν•  수 μžˆλŠ” ν”„λ‘œνΌν‹°μΈ toString()을 μ‚¬μš©ν•  수 μ—†λ‹€.

 

String()은 λͺ¨λ“  νƒ€μž…μ΄ λͺ…μ„Έ κ·œμΉ™μ„ μ •ν™•ν•˜κ²Œ λ”°λ₯΄λ―€λ‘œ, λͺ…μ‹œμ  κ°•μ œ λ³€ν™˜μ„ ν•˜κ³  μ‹Άλ‹€λ©΄ String() ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λŠ” 것이 더 μ’‹λ‹€.


3. μˆ«μžν˜•μœΌλ‘œ λ³€ν™˜

λ‹€λ₯Έ νƒ€μž…μ—μ„œ 숫자 νƒ€μž…μœΌλ‘œμ˜ λ³€ν™˜μ€ ToNumber 좔상 μ—°μ‚° λͺ…μ„Έλ₯Ό κΈ°μ€€μœΌλ‘œ 이루어진닀.

νƒ€μž… 숫자 λ³€ν™˜ κ²°κ³Ό
undefined NaN
null +0
boolean true β‡’ 1, false β‡’ 0
string 숫자둜 λ³€κ²½ λΆˆκ°€λŠ₯ν•œ κ°’ β‡’ NaN, μˆ«μžν˜• λ¬Έμžμ—΄ β‡’ 숫자
Symbol TypeError
BigInt TypeError
object 객체λ₯Ό μ›μ‹œ νƒ€μž…μœΌλ‘œ λ°˜ν™˜(ToPrimitive좔상연산)  β‡’ ToNumber 좔상 μ—°μ‚° μˆ˜ν–‰κ°’ λ°˜ν™˜

 

 

Number(), parseInt() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•΄μ„œ ν˜•λ³€ν™˜μ„ μΌμœΌν‚¨λ‹€.

// Number()
console.log(Number('3'));	//3
console.log(Number(true));	// 1
console.log(NUmber(null));	// 0

// parseInt(λ¬Έμžμ—΄, 기수(μ§„μˆ˜))
console.log(parseInt('10', 10));	// 10
console.log(parseInt('-1', 10));	// -1

console.log(Number('10A', 10));	// NaN
console.log(parseInt('10A', 10));	// 10

 

  • parseInt() ν•¨μˆ˜
    → λ¬Έμžμ—΄λ§Œ λŒ€μƒμœΌλ‘œ λ³€ν™˜ν•œλ‹€.
    값이 λ¬Έμžμ—΄μ΄ μ•„λ‹ˆλΌλ©΄ λ¬Έμžμ—΄λ‘œ λ°˜ν™˜ ν›„ μ‚¬μš©ν•˜λ©°, κ·Έ 과정은 ToString μ—°μ‚° 과정을 λ”°λ₯Έλ‹€.
  • Number()λŠ” λ³€κ²½ λΆˆκ°€λŠ₯ν•œ λ¬Έμžμ— λŒ€ν•΄ λ°”λ‘œ NaN을 λ°˜ν™˜ν•˜μ§€λ§Œ,
    parseInt()λŠ” λΆˆκ°€λŠ₯ν•œ λ¬Έμžκ°€ λ‚˜νƒ€λ‚  λ•ŒκΉŒμ§€ μ΅œλŒ€ν•œ λ³€ν™˜ν•œλ‹€.

4. λΆˆλ¦°ν˜•μœΌλ‘œ λ³€ν™˜

λ‹€λ₯Έ νƒ€μž…μ—μ„œ λΆˆλ¦¬μ–Έ νƒ€μž…μœΌλ‘œμ˜ λ³€ν™˜μ€ ToBoolean 좔상 μ—°μ‚° λͺ…μ„Έλ₯Ό κΈ°μ€€μœΌλ‘œ 이루어진닀.

논리 μ—°μ‚°μžμ˜ truthy/falsy κ°’μ˜ 기쀀도 ToBoolean이닀.

νƒ€μž… λΆˆλ¦¬μ–Έ λ³€ν™˜ κ²°κ³Ό
undefined false
null false
number +0, -0, NaN β‡’ false, κ·Έ μ™Έ 숫자 β‡’ true
string 빈 λ¬Έμžμ—΄ β‡’ false, κ·Έ μ™Έ λ¬Έμžμ—΄ β‡’ true
Symbol true
BigInt 0n β‡’ false, κ·Έ μ™Έ BigInt μ •μˆ˜ β‡’ true
object true

 

 

Boolean(), 이쀑 λΆ€μ • μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•΄μ„œ ν˜•λ³€ν™˜μ„ μΌμœΌν‚¨λ‹€.

const a = null;
const b = 0;
const c = '';
const d = {};
const e = [];

console.log(Boolean(a));	// false
console.log(Boolean(b));	// false
console.log(Boolean(c));	// false
console.log(Boolean(d));	// true
console.log(Boolean(e));	// true

console.log(!!a);	// false
...

 

 


5. μ•”μ‹œμ  κ°•μ œ λ³€ν™˜

μ•”μ‹œμ  κ°•μ œ λ³€ν™˜μ€ μ—°μ‚° 쀑에 λ‚΄λΆ€μ μœΌλ‘œ νƒ€μž…μ„ λ³€ν™˜ν•˜λŠ” 것이닀.

μ—°μ‚°μž κΈ€ >>

 

 

1. λ§μ…ˆ μ—°μ‚°μž

1. ν”Όμ—°μ‚°μž 쀑 ν•˜λ‚˜κ°€ λ¬Έμžμ—΄ νƒ€μž…μΈ 경우, λ‚˜λ¨Έμ§€ νƒ€μž…λ„ λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•˜μ—¬ 병합

2. ν”Όμ—°μ‚°μž 쀑 ν•˜λ‚˜κ°€ 객체이며 λ¬Έμžμ—΄ λ³€ν™˜μ΄ κ°€λŠ₯ν•  경우, λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•˜μ—¬ 병합

3. ν”Όμ—°μ‚°μžκ°€ λͺ¨λ‘ λ¬Έμžμ—΄/객체가 μ•„λ‹Œ 경우, 숫자둜 λ³€ν™˜ν•˜μ—¬ μ—°μ‚°. λ³€ν™˜ 결과의 νƒ€μž…μ΄ 각각 λ‹€λ₯΄λ©΄ TypeError λ°œμƒ

//1
console.log(1 + '');	// '1'

//2
console.log(1 + {});	// '1[Object object]'

//3
console.log(1 + true);	// 2

 

2. 동등 μ—°μ‚°μž

1. ν”Όμ—°μ‚°μž 쀑 ν•˜λ‚˜λŠ” String, ν•˜λ‚˜λŠ” Number인 경우, String β‡’ Number λ³€ν™˜

2. ν”Όμ—°μ‚°μž 쀑 ν•˜λ‚˜λŠ” String, ν•˜λ‚˜λŠ” BigInt인 경우, String β‡’ BigInt λ³€ν™˜

3. ν”Όμ—°μ‚°μž 쀑 ν•˜λ‚˜λŠ” null, ν•˜λ‚˜λŠ” undefined인 경우, λ™λ“±ν•˜κ²Œ νŒλ‹¨

4. ν”Όμ—°μ‚°μž 쀑 ν•˜λ‚˜κ°€ Boolean일 경우, Boolean β‡’ Number λ³€ν™˜

5. ν”Όμ—°μ‚°μž 쀑 ν•˜λ‚˜λŠ” Object, ν•˜λ‚˜λŠ” String/Number/BigInt/Symbol 쀑 ν•˜λ‚˜μΌ 경우, Object β‡’ Primitive λ³€ν™˜

6. ν”Όμ—°μ‚°μž 쀑 ν•˜λ‚˜λŠ” Number, ν•˜λ‚˜λŠ” BigInt인 경우 내뢀적인 숫자 비ꡐ μ•Œκ³ λ¦¬μ¦˜μ— 따라 λΉ„κ΅ν•œ κ²°κ³Ό λ°˜ν™˜

// 1. Str to Num
console.log(1 == '1');	// true

// 2. Str to BigInt
console.log(1n == '1');	// true

// 3. null & undefined
console.log(null == undefined);	// true
console.log(undefined == null);	// true

// 4. Boolean
console.log(true == 1);	// true

// 5. Object to Primitive
console.log('[object Object]' == {});	// true

// 6. Num & BigInt
console.log(1 = 1n);	// true

동등 μ—°μ‚°μ—μ„œμ˜ νƒ€μž… λ³€ν™˜μ€ 가독성이 λ–¨μ–΄μ§€λŠ” κ²½μš°κ°€ μžˆλ‹€.

λ”°λΌμ„œ μ‚¬μš©ν•  λ•Œ νŒ€μ›λ“€κ³Ό μ»¨λ²€μ…˜μ„ 정해두고 μ‚¬μš©ν•˜λŠ” 것이 μ’‹λ‹€.

 

3. 비ꡐ μ—°μ‚°μž

λ¬Έμžμ—΄μ˜ 비ꡐ : κ° 문자λ₯Ό μ•ŒνŒŒλ²³ μˆœμ„œλ‘œ 비ꡐ

console.log('a' < 'b');	// true
console.log('a' < '04');	// true : '1'κ³Ό '0'을 λ¨Όμ € λΉ„κ΅ν•˜κ³ , κ·Έ λ‹€μŒ 문자λ₯Ό 비ꡐ

 

λ°°μ—΄μ˜ 비ꡐ : κ° λ¬Έμžμ—΄λ‘œ λ³€ν™˜λ˜κΈ° λ•Œλ¬Έμ— λ¬Έμžμ—΄λΌλ¦¬μ˜ 비ꡐλ₯Ό μˆ˜ν–‰

console.log(['a'] < ['b']);	// true

 

λ¬Έμžμ—΄ μ™Έμ˜ 비ꡐ : μˆ«μžλ‘œ λ³€ν™˜ν•˜μ—¬ 비ꡐ

console.log('1' < 2n);	// true
console.log(1 < true);	// false
console.log(1n < 2);	// true

 

4. 논리 μ—°μ‚°μž

논리 μ—°μ‚° 결과값은 boolean νƒ€μž…μ΄ 아닐 μˆ˜λ„ μžˆλ‹€.

const a = null;
const b = 'javascript';
const c = 1;

console.log(a && b );	// null
console.log(b || c );	// 'javascript'

// λ¦¬μ•‘νŠΈμ—μ„œ 쑰건에 따라 μ»΄ν¬λ„ŒνŠΈ λ Œλ”λ§ν•  λ•Œ 자주 μ‚¬μš©ν•˜λŠ” 방식
const a = true;
function doSomething() { ... }
...
a && doSomething()	// aκ°€ truthy일 λ•Œλ§Œ ν•¨μˆ˜ μ‹€ν–‰

 

5. 쑰건 ν‘œν˜„μ‹

쑰건 ν‘œν˜„μ‹μ—μ„œλŠ” λͺ¨λ“  값이 boolean으둜 λ³€ν™˜λ˜μ–΄ ν‰κ°€λœλ‹€.

const a = 0;	// μˆ«μžν˜•
const b = 'javascript';	// λ¬Έμžμ—΄ν˜•
const c = null;	// λΆˆλ¦°ν˜•

if (a) {	// falsy
	console.log('ν˜ΈμΆœλ˜μ§€ μ•ŠμŒ');
}

while (b) {	// truthy
	console.log('truthy');
    break;
}

console.log( c ? 'truthy' : 'falsy');	// 'falsy'

 

 

 

 

+ν€΄μ¦ˆ

null == undefined;	// true

false == 0;	// true

0 == [];	// false

String([1, 2, 3]);	// '1, 2, 3'

'a' + {};	// 'a[Object object]'

'1' < ['02'];	// false

NaN === NaN;	// false

[] && 'null' || 'default';	// 'null'

[] || 'default';	// []

const obj = {
	valueOf() {
    	return 1;
    },
    toString() {
    	return 'string'
    }
};

Number(obj);	// 1

 

 

 

 

μ°Έκ³ 

더보기
λ°˜μ‘ν˜•

'Front-end > JavaScript' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

B6 : ꡬ문과 μ—°μ‚°μž  (9) 2025.08.11
μžλ°”μŠ€ν¬λ¦½νŠΈ λͺ¨λ“ˆ μ‹œμŠ€ν…œ  (4) 2025.08.08
B3 : λ³€μˆ˜, μƒμˆ˜, μžλ£Œν˜•  (4) 2025.08.05
B2 : JS κΈ°λ³Έ κ°œλ…  (1) 2025.08.05
λͺ¨λ“ˆ  (0) 2025.03.11