Hey, Amazing ones👋🏼
I am Aryan Sharma and I hope you all are doing great!
This is another blog in the JavaScript course for absolute beginners.
✍🏻I hope this blog course will help you become good at JavaScript.
So, I am starting this blog. Let's get started!!
Type Conversion
Type conversion (or typecasting) means the transfer of data from one data type to another.
Implicit conversion happens when the compiler (for compiled languages) or runtime (for script languages like JavaScript) automatically converts data types.
For example, we have the expression "Ary" + 1
, the Number 1
is implicitly converted into a String and the expression returns "Ary1"
.
String Conversion
String conversion happens when we need the string form of a value.
For example, alert(value)
does it to show the value.
We can also call the String(value)
function to convert a value to a string:
let value = true;
alert(typeof value); // boolean
value = String(value); // value is a string "true"
alert(typeof value); // string
Numeric Conversion
Numeric conversion in mathematical functions and expressions happens automatically.
For example, when the division /
is applied to non-numbers:
alert( "6" / "2" ); // 3
We can use the Number(value)
function to explicitly convert a value
to a number:
let str = "123";
alert(typeof str); // string
let num = Number(str); // becomes a number 123
alert(typeof num); // number
Value | Becomes… |
undefined | NaN |
null | 0 |
true and false | 1 and 0 |
string | Whitespaces (includes spaces, tabs \t , newlines \n etc.) from the start and end are removed. If the remaining string is empty, the result is 0 . Otherwise, the number is “read” from the string. An error gives NaN . |
Boolean Conversion
It happens in logical operations (later we’ll meet condition tests and other similar things) but can also be performed explicitly with a call to Boolean(value)
.
False/Falsy values are:
""
(empty string)0, -0
NaN
undefined
null
false
All other values, except those mentioned above, are truthy values.
Hope you Loved this💓
**
Thanks for Reading this😍
**