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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Using the parseInt() function:

A

let str = “42”;
let num = parseInt(str);
console.log(num); // outputs 42

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Using the parseFloat() function:

A

let str = “42.5”;
let num = parseFloat(str);
console.log(num); // outputs 42.5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Using the unary plus operator +:

A

let str = “42”;
let num = +str;
console.log(num); // outputs 42

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

to convert an object to a number:

A

let obj = { value: 42 };
let num = Number(obj.value);
console.log(num); // outputs 42

How well did you know this?
1
Not at all
2
3
4
5
Perfectly