Search This Blog

2023/09/13

Javascript:Implicit type conversion example

A number stored as string get converted to number if added to itself implicitly.
A number get converted to string if it is concated to empty string implicitly.

Code:
//convert number to string
var x = 1;
x = "" + 1;
console.log(typeof x);
console.log(x);

//convert string to number
var y = "23.78";
y = +y;
console.log(typeof y);
console.log(y);


Output:
string
1
number
23.78

No comments:

Post a Comment