Converting other data types to a number Flashcards
1
Q
Using the Number() function:
A
let str = “42”;
let num = Number(str);
console.log(num); // outputs 42
2
Q
Using the parseInt() function:
A
let str = “42”;
let num = parseInt(str);
console.log(num); // outputs 42
3
Q
Using the parseFloat() function:
A
let str = “42.5”;
let num = parseFloat(str);
console.log(num); // outputs 42.5
4
Q
Using the unary plus operator +:
A
let str = “42”;
let num = +str;
console.log(num); // outputs 42
5
Q
to convert an object to a number:
A
let obj = { value: 42 };
let num = Number(obj.value);
console.log(num); // outputs 42