Converting between types (e.g., String(), Number(), Boolean())
Data type conversion in JavaScript programming helps convert an existing data type variable value from existing data type to another data type format. Here in data type conversion process it can be either implicitly formatted automatically by JavaScript programming or explicitly performed manually by JavaScript programmer or developer. Some of the popular built-in functions in JavaScript programming are String(), Number(), and Boolean() functions are mainly used for explicit conversion of one data program variable data type to other data type format.

Converting to String in JavaScript Programs, String() function.
In JavaScript programming the String() function converts the string data type value declared in a program to string order.
String function syntax.
String(value)
Example of String function in JavaScript.
let integer = 7894;
let str1 = String(integer); // here it use to Converts integer 7894 to string “7894”
console.log(str1); // result is – “7894”
console.log(typeof str1); // result is – “string”
let boolvalue = false;
let boolStr2 = String(boolvalue); // here it Converts boolean false to string “false”
console.log(boolStr2); // result is – “false”
Coercion to String in JavaScript.
When you apply the + operator to a non-string in your JavaScript program, JavaScript implicitly converts the existing data type value in the program to a string (coercion).
let cortesting = 1 + 1 + ” vcanhelpsu”;
console.log(cortesting); // result is – “2 vcanhelpsu” here 1 + 1 is evaluated first, then coerced into a string
Converting to Number in JavaScript, Number() Function.
The built-in Number() function in JavaScript programming programs converts data type variable values to numbers. If a data type value here already has a number value, then the value remains the same in the data type number function conversion process. If data type conversion is not possible, it returns the output value NaN (not a number) in the current program.
Number() syntax in JavaScript.
Number(value)
Number() example in JavaScript program.
let str1 = “78934”;
let integer = Number(str1); // here it use to Converts string “78934” to number 78934
console.log(integer); // result is – 78934
console.log(typeof integer); // result is – “number”
let bool = true;
let boolvalue = Number(bool); // here it use to Converts boolean true to number 1
console.log(boolvalue); // result is – 1
let nonStr = “vcanhelpsu”;
let nonNum = Number(nonStr); // here it used to Converts non string to NaN
console.log(nonNum); // result is – NaN
console.log(isNaN(nonNum)); // result is – true (here it checks if it’s NaN
Coercion to number in JavaScript.
Automatically converts strings to numeric in certain programming tasks in JavaScript programs.
console.log(“3” * 4); // result is – 12 here string “3” is coerced into number 3
console.log(“24” / 3); // result is – 8 here string “24” is coerced into number 24
console.log(“7” – 3); // result is – 4 here string “7” is coerced into number 7
Whereas, the + arithmetic operator has a special behaviour when used with strings in JavaScript, which combines the existing string data type values in the output instead of adding mathematical values in the JavaScript program.
console.log(“3” + 9); // result is – “39” here string “3” is concatenated with number 9
Converting to Boolean in JavaScript, boolean() function.
The boolean() function in JavaScript programs converts a data type variable value into its corresponding boolean data value by representing it.
Syntax of Boolean function in JavaScript.
boolean(value)
Example of Boolean function.
let text = “Vcanhelpsu”;
let boolvalue = Boolean(text); // here Non-empty string becomes true boolean value
console.log(boolvalue); // result is – true
let emptytext = “”;
let emptytextBool = Boolean(emptytext); // here a Empty string becomes false boolean value
console.log(emptytextBool); // result is – false
let integer = 0;
let intBool = Boolean(integer); // here Zero becomes false boolean value
console.log(intBool); // result is – false
let objdata = {};
let objBoolvalue = Boolean(objdata); // here Non-null object data type value becomes true
console.log(objBoolvalue); // result is – true
Summary of Conversions String(), Number(), Boolean() functions in JavaScript.
Conversion Function | Converts to… | Example | Result |
String(value) function | It used to convert String to other data type | String(789) | “789” |
Number(value) function | It used to convert Number to number data type | Number(“4987”) | 4987 |
Boolean(value) function | It used to convert Boolean (truthy/falsy) Boolean value | Boolean(“vcanhelpsu”) | vcanhelpsu |
Conclusion of String(), Number(), Boolean() functions in JavaScript.
- String() function – Converts data variable value to string in any JavaScript program.
- Number() function – Converts data variable value to numeric data type in any JavaScript program. Here if data type conversion is not possible, it returns NaN value in the output.
- Boolean() function – Converts the data variable value in any JavaScript program to boolean data type value, here false value gives false result, similarly true value gives true result.