ν λ³νμ μμ λ³μμ νμ μ μ μ μλ λ°©λ²μ μμ λ³΄κ² μ΅λλ€.
κ°λ¨ν©λλ€. <typeof λ³μλͺ > μ μ¬μ©νλ©΄ λ³μμ νμ μ μ μ μμ΅λλ€.
boolean ν, μ«μνμ λ¬Έμν μΌλ‘ λ°κΏ λ΄ λλ€.
let value = true;
alert(typeof value); // boolean
value = String(value); // string μΌλ‘ ν λ³ν
alert(value); // "true"
alert(typeof value); // string
String() μ μ¬μ©νμ§ μκ³ +"" λ₯Ό μ¬μ©νκΈ°λ ν©λλ€. νΉμ toString() μ μ¬μ©ν©λλ€.
let cnt = 1;
console.log(typeof cnt); // number
console.log(typeof (cnt+"")); // string
console.log(cnt.toString()); // string
(cnt+"") μμ κ΄νΈλ₯Ό λΉΌλ©΄ μμ cnt μ νμ μ΄ λμ€λ κ΄νΈλ‘ μ°μ μμλ₯Ό μ€μΌ ν©λλ€.
λ¬Έμνμ μ«μν μΌλ‘ λ°κΏ λ΄ λλ€.
alert( "15" / "5" ); // 3, λ¬Έμμ΄μ΄ μ«μνμΌλ‘ μλλ³νλ ν μ°μ°μ΄ μνλ©λλ€.
let str = "123";
alert(typeof str); // string
let num = Number(str); // 123
alert(typeof num); // number
μλ λ³νλλ€κ³ μ무 κ°μ΄λ λ£μΌλ©΄ μλ©λλ€.
alert("15" / "a5"); // NaN : not a number μ«μκ° μλλΌκ³ μΆλ ₯λ©λλ€.
λ¬Έμνμ΄ λ€μ΄κ°λ©΄ NaN μ΄ λμ΅λλ€.
Boolean νμΌλ‘ λ³ν ν©λλ€.
alert(Boolean(1)); // μ«μ 1(true)
alert(Boolean(0)); // μ«μ 0(false)
alert(Boolean("helloWorld!")); // λ¬Έμμ΄(true)
alert(Boolean("")); // λΉ λ¬Έμμ΄(false)
λ¨μν true, false λ§ λΆλ¦°νμΌλ‘ λ°κΏ μ μλ κ²μ΄ μλλΌ μ«μλ λ¬Έμνλ μ΄λ€ κ°μ΄λμ λ°λΌ true, false μͺ½μΌλ‘ λλ μ§λλ€. null μ΄λ undefined λ λλ μ§ μ μλλ°, μ΄κ²μ μκ³ μμΌλ©΄ κ΅μ₯ν μ μ©ν©λλ€!
JS Truthy μ Falsy https://lazygay.tistory.com/10
JS Truthy μ Falsy
μ«μλ λ¬Έμμ΄λ μ΄λ€ κ°μ΄λμ λ°λΌ true, false μ κ°μ κ°μ§λλ€. μ΄κ²μ μ ννκ² μκ³ μμ΄μΌ λμ€μ ν·κ°λ¦¬μ§ μμΌλ μ΄λ² κΈ°νμ μΈμ λ΄ μλ€! Falsy κ±°μ§ 0 -0 '' null undefined NaN Truthy μ°Έ 1 -1 'hel.
lazygay.tistory.com
μ κ²μκΈμ μ°Έκ³ ν΄ μ£ΌμΈμ!
boolean νμΌλ‘ λ°κΎΈλ λ κ°λ¨ν λ°©λ²λ μμ΅λλ€. μμ λλν 2κ°λ₯Ό λΆμ΄λ©΄ λ©λλ€.
alert(!!1); // true
alert(!!0); // false
μ°Έκ³ μλ£
https://ko.javascript.info/type-conversions
ν λ³ν
ko.javascript.info
https://developer.mozilla.org/en-US/docs/Web/API/Window/alert
Window.alert() - Web APIs | MDN
window.alert() instructs the browser to display a dialog with an optional message, and to wait until the user dismisses the dialog.
developer.mozilla.org
'JavaScript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
JS ν¨μ μ μΈλ¬Έκ³Ό ν¨μ ννμ μ°¨μ΄ (0) | 2022.08.11 |
---|---|
JS μμ 볡μ¬μ κΉμ λ³΅μ¬ (0) | 2022.08.11 |
JS λΆλ³ κ°μ²΄ λ§λ€κΈ° (0) | 2022.08.10 |
JS Truthy μ Falsy (0) | 2022.08.10 |
JS κΈ°λ³Έν λ°μ΄ν°μ μ°Έμ‘°ν λ°μ΄ν°μ μ°¨μ΄ (0) | 2022.08.10 |