Hello Readers👋
I hope you are doing great...
In this JavaScript for Beginners series chapter we will cover the 3 main primitive type conversions: to string, to number and to boolean
.
Converting types
Most of the time, operators and functions in JavaScript automatically values into the right type. For example, mathematical operators will convert values to number in order to perform calculations.
However, sometimes you need to explicitly convert a value to a specific type. In this chapter we will cover some primitive type conversions.
String Conversion
There are a few ways to convert a value to a string but the most straight forward is by using the String()
function.some build in JavaScript functions such as alert() will always convert the value to a string in order to display it.
Examples
let numToString = String(1);
// Output: "1"
let trueToString = String(true);
// Output: "true"
let falseToString = String(false);
// Output: "false"
let nullToString= String(null);
// Output: "null"
let undefinedToSTring = String(undefined);
// Output: "undefined"
let arrayToString = String([1,2,3]);
// Output: "1,2,3"
Numeric Conversion
Numeric conversions are useful when the value is read from a string based source where a number is expected. Mathematically functions and expressions will automatically convert string value into numbers, but it can also be achieved using the Number()
function.
Examples
let stringToNumber = Number("123");
// Output: 123
let wordToNumber = Number("Hello");
// Output: NaN
let clacToNumber = "10"/"2"
// Output: 5
let trueToNumber = Number(true);
// Output: 1
let falseToNumber = Number(false);
// Output: 0
let nullToNumber = Number(null);
// Output: 0
let undefinedToNumber = Number(undefined);
// Output: NaN
let emptyStringToNumber = Number("");
// Output: 0
Boolean Conversion
Boolean conversions are quite simple and happen mostly in logical operations but can also be done by using the Boolean()
function. Values that are considered "empty" (Such as 0, null, NaN, undefined and empty strings) become false and other values become true.
Double exclamation marks can also be used as a shorthand boolean conversion.
Examples
let stringToBoolean = Boolean("Hello");
// Output: true
let stringFalseToBoolean = Boolean("false");
// Output: true
let emptyStringToBoolean = Boolean("");
// Output: false
let numberToBoolean = Boolean("123");
// Output: true
let nullToBoolean = Boolean(null);
// Output: true
let zeroToBoolean = Boolean(0);
// Output: false
let oneToBoolean = Boolean(1);
// Output: true
// Shorthand conversion
let value = "Hello";
console.log(!!value);
// Output: true
Summary
The most widely used conversions are to string,to number and to boolean.
String conversion occurs when we output something and is usually obvious.
Numeric conversion occurs in mathematical operations.
Boolean conversion occurs in logical operations.
Make sure you check back for the next chapter which will be all about basic operators!👀
Hope you found this useful.🙂