Type Conversion in Javascript
In JavaScript, type conversion is the process of converting a value from one data type to another. There are two types of type conversion: implicit type conversion and explicit type conversion.
- Implicit Type Conversion: Implicit type conversion, also called type coercion, occurs automatically when values are used in operations where the types of the values do not match. For example, if we add a number and a string, JavaScript will attempt to convert the string to a number before performing the addition operation. Here are some examples:
console.log(5 + '5'); // Output: '55' console.log('5' - 1); // Output: 4 console.log('5' * 2); // Output: 10 console.log(true + 1); // Output: 2
In the above examples, JavaScript implicitly converts the string '5' to a number when performing arithmetic operations. It also converts the boolean value
true
to the number1
before performing addition. - Explicit Type Conversion: Explicit type conversion, also called type casting, is when a value is manually converted from one type to another using built-in functions or operators. Here are some examples:
let num = 5; let str = String(num); // convert number to string console.log(typeof str); // Output: 'string' let str2 = '123'; let num2 = Number(str2); // convert string to number console.log(typeof num2); // Output: 'number' let bool = Boolean(num); // convert number to boolean console.log(bool); // Output: true
In the above examples, we use the built-in functions String()
, Number()
, and Boolean()
to explicitly convert values from one type to another.
Note that some values can be converted to other types more easily than others. For example, a string containing only digits can be easily converted to a number using the Number()
function. However, a string containing non-numeric characters cannot be converted to a number, and will result in NaN
(Not a Number).
Understanding type conversion is important in JavaScript because it can affect the behavior of our programs. By knowing how and when type conversion occurs, we can write more reliable and predictable code.
Type Conversion Methods
In JavaScript, there are several built-in functions that can be used for type conversion. Here are some common methods for type conversion in JavaScript:
Number()
: Converts a value to a number. If the value cannot be converted to a number,NaN
is returned.let num1 = Number('123'); // num1 is now the number 123 let num2 = Number('hello'); // num2 is now NaN
String()
: Converts a value to a string.let str1 = String(123); // str1 is now the string "123" let str2 = String(false); // str2 is now the string "false"
Boolean()
: Converts a value to a boolean.let bool1 = Boolean(''); // bool1 is now false let bool2 = Boolean(0); // bool2 is now false let bool3 = Boolean('hello'); // bool3 is now true let bool4 = Boolean(123); // bool4 is now true
parseInt()
: Parses a string and returns an integer. If the string cannot be converted to an integer,NaN
is returned.let num1 = parseInt('123'); // num1 is now the number 123 let num2 = parseInt('hello'); // num2 is now NaN let num3 = parseInt('10.5'); // num3 is now the number 10
parseFloat()
: Parses a string and returns a floating-point number. If the string cannot be converted to a number,NaN
is returned.let num1 = parseFloat('3.14'); // num1 is now the number 3.14 let num2 = parseFloat('hello'); // num2 is now NaN let num3 = parseFloat('10.5'); // num3 is now the number 10.5
JSON.parse()
: Parses a JSON string and returns a JavaScript object.let jsonStr = '{"name": "John", "age": 30}'; let obj = JSON.parse(jsonStr); // obj is now the object {name: "John", age: 30}
These methods can be used to convert values from one type to another in JavaScript, and are commonly used in programming to ensure that the correct data types are being used in operations and functions.
Typeof Operator
typeof operator is used to check data type.
let a = "name";
let b = 220;
let c = ["php", "javascript", "python"];
let d = false;
let e = { course: "javascript", price: "FREE" };
let f = new Date();
let g = null;
let h = NaN;
let i = function () { };
let j = undefined;
console.log(typeof (a)); //string
console.log(typeof (b)); //number
console.log(typeof (c)); //object
console.log(typeof (d)); //boolean
console.log(typeof (e)); //object
console.log(typeof (f)); //object
console.log(typeof (g)); //object
console.log(typeof (h)); //number
console.log(typeof (i)); //function
console.log(typeof (j)); //undefined