Created
July 20, 2017 09:05
-
-
Save alordiel/86f35bf65615ed09a581446de47c89e8 to your computer and use it in GitHub Desktop.
JS Convering one type of variable to another type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var str = "string"; | |
var num = 123; | |
var boo = false; | |
var obj = {h:3}; | |
console.log(String(str)); // "string" | |
console.log(Number(str)); // NaN | |
console.log(Boolean(str)); // true | |
console.log(Object(str)); // "string" | |
console.log(String(num)); // "123" | |
console.log(Number(num)); // 123 | |
console.log(Boolean(num)); // true | |
console.log(Object(num)); // 123 | |
console.log(String(boo)); // "false" | |
console.log(Number(boo)); // 0 | |
console.log(Boolean(boo)); // false | |
console.log(Object(boo)); // true | |
console.log(String(obj)); // "[object Object]" | |
console.log(Number(obj)); // NaN | |
console.log(Boolean(obj)); // true | |
console.log(Object(obj)); // {h:3} | |
// Cases with undefined and null | |
console.log(String(null)); // "null" | |
console.log(Number(null)); // 0 | |
console.log(Boolean(null)); // false | |
console.log(Object(null)); // [object Object] { ... } | |
console.log(String(undefined)); // "undefined" | |
console.log(Number(undefined)); // NaN | |
console.log(Boolean(undefined)); // false | |
console.log(Object(undefined)); // [object Object] { ... } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment