top

Search

JavaScript Tutorial

Most of the time, operators and functions automatically convert a value to the correct type. That’s called “type conversion”.For example, alert automatically converts any value to a string to show it. Mathematical operations convert values to numbers.There are also cases when user need to explicitly convert a value to put things right.String Conversion in JavascriptString conversion happens when we need the string form of a value.For example, alert(value) does it to show the value. The conversion here is done automatically. We can also use a call String(value) function for that conversion manually.let value = true; alert(typeof value); // boolean value = String(value); // now value is a string "true" alert(typeof value); // stringString conversion is mostly obvious. A true becomes "true", 0 becomes "0".Number ConversionNumeric conversion happens in mathematical functions and expressions automatically.For example, when division / is applied to non-numbers, which can be converted:console.log("12"/"2"); //6 – Both strings are converted to NumberWe can use a Number(value) function to explicitly convert a value to Number. Like below:let str = "123"; let num = Number(str); console.log(typeof str); //string console.log(typeof num); //numberIf the string to be converted is not a valid number, the result of such conversion is NaN, for example:let str = "One two three"; let num = Number(str); console.log(num); //NaNSome other numeric conversion rules for explicit conversation are below//undefined becomes null let str; let num = Number(str); console.log(str); //undefined console.log(num); //NaN //null becomes 0 let str = null; let num = Number(str); console.log(str); //null console.log(num); //0 //true/false becomes 1/0 let str1 = true; let str2 = false; let num1 = Number(str1); let num2 = Number(str2); console.log(str1); //true console.log(num1); //1 console.log(str2); //false console.log(num2); //0 //In strings whitespaces from the start and the end are removed. Then, if //the remaining string is empty, the result is 0. Otherwise, the number is //“read” from the string. An error gives NaN if the string cannot be //converted. let str1 = "       "; let str2 = "  123 "; let str3 = "  123z "; let num1 = Number(str1); let num2 = Number(str2); let num3 = Number(str3); console.log(num1); //0 console.log(num2); //123 console.log(num3); //NaNProblem with addition operator(+)All mathematical operations convert values to numbers but not the addition(+) operator. In JavaScript it is used for string concatenation also. So, if one of the added values is a string then the other one is also converted to a string. Then it concatenates them:console.log(1 + "2"); //12 //1 gets converted to string and so "1" + "2" becomes "12" console.log(4 + 5 + "3");  //93 //First 4 + 5 gives 9. Since "3" is string, so 9 is converted to "9" and //the result becomes "93" console.log("3" + 5 + 4);  //354 //As with above cases "3" + 5 gives "35" and then "35" + 4 gives "354"Boolean ConversionBoolean conversion is very simple.  It follows two basic rules.Values that are “empty”, like 0, an empty string, null, undefined and NaN become false.Other values become true.console.log(Boolean(0)); //false console.log(Boolean(1)); //true console.log(Boolean("0")); //true – since non-empty string console.log(Boolean("")); //false console.log(Boolean(undefined)); //false console.log(Boolean(null)); //false console.log(Boolean(NaN)); //false
logo

JavaScript Tutorial

Type Conversions in Javascript

Most of the time, operators and functions automatically convert a value to the correct type. That’s called “type conversion”.

For example, alert automatically converts any value to a string to show it. Mathematical operations convert values to numbers.

There are also cases when user need to explicitly convert a value to put things right.

String Conversion in Javascript

String conversion happens when we need the string form of a value.

For example, alert(value) does it to show the value. The conversion here is done automatically. We can also use a call String(value) function for that conversion manually.

let value = true;
alert(typeof value); // boolean
value = String(value); // now value is a string "true"
alert(typeof value); // string

String conversion is mostly obvious. A true becomes "true", 0 becomes "0".

Number Conversion

Numeric conversion happens in mathematical functions and expressions automatically.

For example, when division / is applied to non-numbers, which can be converted:

console.log("12"/"2"); //6 – Both strings are converted to Number

We can use a Number(value) function to explicitly convert a value to Number. Like below:

let str = "123";
let num = Number(str);
console.log(typeof str); //string
console.log(typeof num); //number

If the string to be converted is not a valid number, the result of such conversion is NaN, for example:

let str = "One two three";
let num = Number(str);
console.log(num); //NaN

Some other numeric conversion rules for explicit conversation are below

//undefined becomes null
let str;
let num = Number(str);
console.log(str); //undefined
console.log(num); //NaN
//null becomes 0
let str = null;
let num = Number(str);
console.log(str); //null
console.log(num); //0
//true/false becomes 1/0
let str1 = true;
let str2 = false;
let num1 = Number(str1);
let num2 = Number(str2);
console.log(str1); //true
console.log(num1); //1
console.log(str2); //false
console.log(num2); //0
//In strings whitespaces from the start and the end are removed. Then, if //the remaining string is empty, the result is 0. Otherwise, the number is //“read” from the string. An error gives NaN if the string cannot be //converted.

let str1 = "       ";
let str2 = "  123 ";
let str3 = "  123z ";
let num1 = Number(str1);
let num2 = Number(str2);
let num3 = Number(str3);
console.log(num1); //0
console.log(num2); //123
console.log(num3); //NaN

Problem with addition operator(+)
All mathematical operations convert values to numbers but not the addition(+) operator. In JavaScript it is used for string concatenation also. So, if one of the added values is a string then the other one is also converted to a string. Then it concatenates them:

console.log(1 + "2"); //12
//1 gets converted to string and so "1" + "2" becomes "12"
console.log(4 + 5 + "3");  //93
//First 4 + 5 gives 9. Since "3" is string, so 9 is converted to "9" and //the result becomes "93"
console.log("3" + 5 + 4);  //354
//As with above cases "3" + 5 gives "35" and then "35" + 4 gives "354"

Boolean Conversion

Boolean conversion is very simple.  It follows two basic rules.

  • Values that are “empty”, like 0, an empty string, null, undefined and NaN become false.
  • Other values become true.
console.log(Boolean(0)); //false
console.log(Boolean(1)); //true
console.log(Boolean("0")); //true – since non-empty string
console.log(Boolean("")); //false
console.log(Boolean(undefined)); //false
console.log(Boolean(null)); //false
console.log(Boolean(NaN)); //false

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Aditya

The tutorial provides the more information JavaScript this provides the end to end information about JS and also they provides the more detailed concepts of loops, functions, objects, arrays this will use us to buld the effective web site. Thanks for the team good job.

Suggested Tutorials

Node JS Tutorial

Build various types of web applications,command-line application,etc.
Node JS Tutorial

Build various types of web applications,command-line application,etc....

Read More

Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introduced by Google corporation in 2012 and was considered to be one of the most promising among JavaScript frameworks. It was written completely in JavaScript to separate an application’s logic from DOM manipulation, aiming at dynamic page updates. Angular introduced many powerful features enabling the developer to effortlessly create rich and single-page applications.Topics CoveredThis Angular tutorial will span over eight modules, each module covering numerous individual aspects that you need to gain complete information about Angular. This set of modules serves as an Angular tutorial for beginners along with experienced IT professionals.Here are the topics that will be covered in the Angular tutorial:Get started with Angular.Learn the basics of Angular.Know what Angular Directives.Get an idea of Component Inputs and Outputs of Angular.Know about Forms in Angular.About Services in Angular.Pipes in Angular.HTTP, Routing and Building in Angular.Who can benefit from this tutorial?This Angular tutorial will be helpful to IT professionals such as:Software Developers, Web Application Programmers and IT Professionals Software Architects and Testing Professionals Career aspirants in web development
Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introdu...

Read More