JavaScript Type Conversions and Checking Data type
- To check the data type of a certain variable we use the typeof method.
- Type conversion is the process of converting data of one type to another
example: checking data type.
let firstName = 'naveen' // string
console.log(typeof firstName) // string
There are two types of type conversion in JavaScript.
- Implicit Conversion - automatic type conversion
- Explicit Conversion - manual type conversion
JavaScript Implicit Conversion
- JavaScript automatically converts one data type to another.
- This is known as implicit conversion.
Examples
Implicit Conversion to String
// numeric string used with + gives string type
let result;
result = '3' + 2;
console.log(result) // "32"
result = '3' + true;
console.log(result); // "3true"
result = '3' + undefined;
console.log(result); // "3undefined"
result = '3' + null;
console.log(result); // "3null"
- When a number is added to a string, JavaScript converts the number to a string before concatenation.
Implicit Conversion to Number
let num;
num = '4' - '2';
console.log(num); // 2
num = '4' - 2;
console.log(num); // 2
num = '4' * 2;
console.log(num); // 8
num = '4' / 2;
console.log(num); // 2
Non-numeric String Results to NaN
let str;
str = 'hello' - 'world';
console.log(str); // NaN
str = '4' - 'hello';
console.log(str); // NaN
Implicit Boolean Conversion to Number
- if boolean is used, true is 1, false is 0
let bol;
bol = '4' - true;
console.log(bol); // 3
bol = 4 + true;
console.log(bol); // 5
bol = 4 + false;
console.log(bol); // 4
- JavaScript considers 0 as false and all non-zero number as true. And, if true is converted to a number, the result is always 1.
null Conversion to Number
let nul;
nul = 4 + null;
console.log(nul); // 4
nul = 4 - null;
console.log(nul); // 4
undefined used with number, boolean, or null
- Arithmetic operation of undefined with number, boolean, or null gives NaN
let und;
und = 4 + undefined;
console.log(und); // NaN
und = 4 - undefined;
console.log(und); // NaN
und = true + undefined;
console.log(und); // NaN
und = null + undefined;
console.log(und); // NaN
JavaScript Explicit Conversion
- You can also convert one data type to another as per your needs
- The type conversion that you do manually is known as explicit type conversion.
- explicit type conversions are done using built-in methods
- Here are some common methods of explicit conversions
Convert to Number Explicitly
- To convert numeric strings and boolean values to numbers, you can use Number().
let numexplict;
// string to number
numexplict = Number('324');
console.log(numexplict); // 324
numexplict = Number('324e-1')
console.log(numexplict); // 32.4
// boolean to number
numexplict = Number(true);
console.log(numexplict); // 1
numexplict = Number(false);
console.log(numexplict); // 0
numexplict = Number(null);
console.log(numexplict); // 0
numexplict = Number(' ')
console.log(numexplict); // 0
Convert to String Explicitly
- To convert other data types to strings, you can use either String() or toString()
let strg;
strg = String(324);
console.log(strg); // "324"
strg = String(2 + 4);
console.log(strg); // "6"
//other data types to string
strg = String(null);
console.log(strg); // "null"
strg = String(undefined);
console.log(strg); // "undefined"
- String() takes null and undefined and converts them to string. However, toString() gives an error when null is passed.
Convert to Boolean Explicitly
- To convert other data types to a boolean, you can use Boolean().
- In JavaScript, undefined, null, 0, NaN, '' converts to false
let bolexplict;
bolexplict = Boolean('');
console.log(bolexplict); // false
bolexplict = Boolean(0);
console.log(bolexplict); // false
bolexplict = Boolean(undefined);
console.log(bolexplict); // false
bolexplict = Boolean(null);
console.log(bolexplict); // false
bolexplict = Boolean(NaN);
console.log(bolexplict); // false
0 Comments:
Post a Comment
Do leave your comments