Can we change the Datatype of variable in javascript . | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can we change the Datatype of variable in javascript .

Ex : x= "1"; Here x is string. Can " 1" String convert into Number 1.

5th May 2020, 10:27 AM
Ashutosh
Ashutosh - avatar
5 Answers
+ 6
Yes why not Just use the parseInt function Var x ="1"; Var num=parseInt (x); //Now you can do arithmetical operations with it. You can also convert it into float by using parsefloat function
5th May 2020, 11:01 AM
Shreyansh
Shreyansh - avatar
+ 3
Yes why not use perseInt() function
5th May 2020, 10:32 AM
Ayush Kumar
Ayush Kumar - avatar
+ 3
Hello Ashu. Sure you can :)) const x = "1"; // here 1 is a string const num = Number(x); // now 1 becomes a number console.log(num); Use the Number function to make a string-number to number!
5th May 2020, 10:39 AM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
0
Ayush. Ks Arb rahim badsa I have try both 1. Number(x) 2. perselnt() it work Thanks.
5th May 2020, 10:51 AM
Ashutosh
Ashutosh - avatar
0
Thanks Shreyansh bro.
5th May 2020, 12:36 PM
Ashutosh
Ashutosh - avatar