*Changing value from one data types to another *
conversion of the value of the declare variable to a different data types can be done by ensuring that the data type to be converted to, is the one used and begins with a Capital letter. For example:
let year = String(1980);
console.log(year); //"1980"
Generally: let x = Data-types(value);
List of Data-types
- Number
- String
- Boolean e.t.c
JavaScript has some in built conversion one can explore without the use of methods:
let result;
//For example =>
// String conversion
result = '3' + 2; // "32"
result = '10' + true; //"10true"
//Number conversion
result = '10' - '2'; // 8
result = '10' - 2; //8
//NaN conversion
result = 'hello' - 'world'; // NaN
result = '4' - 'hello'; //NaN
//Boolean conversion
result = '8' - true; // 7
result = 4 + true; //5
//Null conversion
result = 15 + null; // 15
result = 4 - null; //4
To check what type of data type, make use of typeof
console.log(typeof(num)); //Number
while with method
/* convert to number */
string can be converted to number using Number
keyword even other data types such as boolean and null in JavaScript.
for example:
true
is known as 1
while false
, empty string ""
and null
is 0
or empty value.
A text, NaN
and undefined
will return Not a number NaN
/* method in Number*/
some specific method can be used to convert one data types to a number. Method like:
1. parseInt()
2. parseFloat()
3. /*unary operator*/ +""
4. Math.floor()
/* convert to string */
conversion to string is done with String /*or*/ toString()
. example:
/* convert to boolean */
empty string
, 0
, undefined
, NaN
and null
return false
when passed with boolean but others return true
.
example:
Let us connect with each other
connection between two or more parties bring solution to a problem
Top comments (0)