TypeConversion In Js
September 12, 2019
Meaning Of TypeConversion In js
It Means That The Converting One Type Of DataType Into Another Typeof It Happens By Two Types Implicitly and Explicitly
-
Implicit TypeConversion :-
It happens dynamiclly but the value must be same and datatype can of any type it return either true or false depend upon what value you enters
Eg:-
==(doubble Equal too)
“10”==10 true
-
Explicit TypeConversion :-
We Have To Implement It Manually In Our Conversion. There Are Many Predefined Functions Which Are Used for The Explicit Conversion Explain Below
- To String (""):-
String();
Eg:- String(10); “10”
- To Number (1,2,3 Etc.):-
Number();
Eg:- Number(“10”); 10
- To Boolean (true,false):-
Boolean();
Eg:- Boolean(1); true
Boolean(0); false
Summary And Additional Information:-
- *Numeric conversion happens in mathematical functions and expressions automatically.
For example, when division / is applied to non-numbers:
Console.log( “6” / “2” ); // 3, strings are converted to numbers*
- Addition ‘+’ concatenates strings Almost all mathematical operations convert values to numbers. A notable exception is addition +. If one of the added values is a string, the other one is also converted to a string.
Then, it concatenates (joins) them:
console.log( 1 + ‘2’ ); // ‘12’ (string to the right) console.log( ‘1’ + 2 ); // ‘12’ (string to the left)