7554425D-0054-440D-B95E-D2ABC13D62CD Created with sketchtool.
{body}
Your Saved Info Packages

View your saved Course or Program Packages containing pricing and detailed curriculum.

Speak with a Learning Advisor.

Have any questions? We'll call you.

Fill out the form below and a Learning Advisor will reach out at a time convenient for you.

Please pick a valid date and time between 9 AM and 8 PM eastern (Monday to Friday)

By clicking "Book a call," you accept our Terms and will also receive exclusive offers and updates about new courses, workshops and events.

JavaScript Tutorial

JavaScript Casting

Ready to start your career in Development? Find out more about BrainStation's Web Development Bootcamp

JavaScript provides multiple data types and often Developers find themselves stuck when dealing with a data type that was meant to be of a particular type but it isn’t. JavaScript provides a very easy way to change the data type of a certain value from one to another considering the fact that it is a valid change. This is called Casting or Type Casting in JavaScript where a value is casted or changed from being of one type to another.

Type Casting sometimes happens automatically or implicitly and sometimes as a Developer you have to specify that a type casting needs to be performed. Let’s look at both Implicit and Explicit Type Conversions.

Implicit Type Casting

Implicit Type Conversion to Number

As you can see in example below, when a string type is used with a number type in a mathematical expression using mathematical operators, the string numbers are changed into number type implicitly before performing the operation and the resulting value is then a valid number data type. If a valid numeric conversion is not possible implicitly, it results in the value being Not a Number or NaN.

```
let result = ‘38’ + ‘20’;
console.log(result); // prints 58
console.log(typeof result); // prints number

let result = ‘38’ - 20;
console.log(result); // prints 18
console.log(typeof result); // prints number

let result = ‘38’ * 10; 
console.log(result); // prints 380
console.log(typeof result); // prints number

let result = ‘38’ / 2; 
console.log(result); // prints 19
console.log(typeof result); // prints number

let result = ‘38’ / ‘hello’; 
console.log(result); // prints NaN

```

Implicit Type Conversion to String

As you can see in the example below, when a string value is added to a number, it results in the number value to be changed into string and the resulting value to be of type string where both values are concatenated.

```
let result = ‘38’ + 20;
console.log(result); // prints ‘3820’
console.log(typeof result); // prints string
```

Explicit Type Casting

Explicit Type Conversion to Number

As seen in the example below, one can use the Number() constructor function to change a value that is non-numeric like boolean or a number in string to be of number data type.

```
let value = ‘38’;
let result = Number(value);
console.log(result); // prints 38
console.log(typeof result); // prints number

let value = false;
let result = Number(value);
console.log(result); // prints 0 as false converts to 0
console.log(typeof result); // prints number

let value = true;
let result = Number(value);
console.log(result); // prints 1 as true converts to 1
console.log(typeof result); // prints number
```

Explicit Type Conversion to String

As seen in the example below, one can use the String() constructor function to change a value that is numeric or boolean to be of type String.

```
let value = 38;
let result = String(value);
console.log(result); // prints ‘38’
console.log(typeof result); // prints string

let value = false;
let result = String(value);
console.log(result); // prints ‘false’
console.log(typeof result); // prints string

let value = 20 + 10;
let result = String(value);
console.log(result); // prints ‘30’
console.log(typeof result); // prints string
```

Explicit Type Conversion to Boolean

In JavaScript, number 0 relates to false and number 1 relates to true. A string that is empty ’’ relates to false and a string with characters in it or the one which is not empty relates to true. This is because everything in JavaScript is either truthy or falsy. Here is a chart that explains the truthy-falsy nature of JavaScript. A value that is a number or string can be converted into its own boolean representation of being truthy or falsy by using the Boolean constructor function Boolean().

```
let value = ‘hello’;
let result = Boolean(value);
console.log(result); // prints true as value is not empty string
console.log(typeof result); // prints boolean

let value = 0;
let result = Boolean(value);
console.log(result); // prints false as 0 converts to false
console.log(typeof result); // prints boolean

let value = 1;
let result = Boolean(value);
console.log(result); // prints true as 1 converts to true
console.log(typeof result); // prints boolean
```
Set Password

You already have an account with BrainStation, but you still need to set up a password.