Ask coding questions

← Back to all posts
How do I do this in javascript?
ia13ru (43)

I have a variable. It has the value "7", and I want to convert the value into 7. How do I do this?

Answered by Wumi4 (486) [earned 5 cycles]
View Answer
Comments
hotnewtop
Wumi4 (486)

Use parseInt().

let seven = "7" // Define 'seven' is "7"
seven = parseInt(seven) // Now 'seven' is 7.
ia13ru (43)

@Wumi4 Can you do it similarly with ""string"" and "variable"?

Baconman321 (1060)

@Wumi4 Number() also works fine.

Baconman321 (1060)

@ia13ru That would convert to NaN because "string" is not a number. "string" isn't a number it's a word, so it can't be converted into one.