Wednesday, August 18, 2021

String Conversion and Coercion in Javascript Language

String Conversion - String conversion means when we get number input from user in a String Format. Ex '1995'

So for this input we can convert this String to Number Using Javascript inbuilt function called Number();

let userInput = '1992'; // This is String becoz of Number in Quote.


console.log(userInput + 10); // This will Print 199210 because of 1992 is String and plus sign link both.


// Convert that string in Number Format Using Number();


console.log(Number(userInput) + 10) // This will Print 2002 because userInput is Number now so we can add 10



// You Can Check String or Number Using typeof keyword in Javascript.


console.log(typeof Number(userInput)); // This will print Number
console.log(typeof userInput); // This will Print String

String Coercion : String Coercion means That Javascript will automatic Convert Number to String

const myAge = 31; // Which is Number Actually.
console.log('My Age is ' + 31 + ' Years'); // This will convert 31 into string automatic

0 comments

Post a Comment