DEV Community

Cover image for JavaScript Type Conversion
Bello Osagie
Bello Osagie

Posted on • Edited on

5 3

JavaScript Type Conversion


Type Conversion

Type conversion is the conversion from one data type to another.

Below are the data type conversions:

String Conversion

Conversions from one data type to a string are possible with the String() function in use.

For example, conversion from a boolean to a string:

script.js

const bool = true; // try undefined, null, NaN, etc
console.log(typeof bool); // boolean

const toStr = String(bool);
toStr; // 'true'
console.log(typeof toStr); // string
Enter fullscreen mode Exit fullscreen mode

Number Conversion

Conversions from one data type to a number are possible with the Number() function in use.

For example, conversion from a boolean to a number:

script.js

const bool = true; // try undefined, null, NaN, etc
console.log(typeof bool); // boolean

const toNumber = Number(bool);
toNumber; // 1;
console.log(typeof toNumber); // number
Enter fullscreen mode Exit fullscreen mode
Data Types Numeric Conversion
true and false 1 and 0
string NaN
' ' NaN
null, undefined, and NaN number

script.js

const notNum = NaN; // try undefined, null, NaN, etc
console.log(typeof notNum); // number

const toNumber = Number(notNum);
toNumber; // NaN;
console.log(typeof toNumber); // number
Enter fullscreen mode Exit fullscreen mode

Boolean Conversion

Conversions from one data type to a boolean are possible with the Boolean() function in use.

For example, conversion from a number to a boolean:

script.js

const num = 0; // try undefined, null, NaN, etc
console.log(typeof num); // number

const toBoolean = Boolean(num);
toBoolean; // false;
console.log(typeof toBoolean); // boolean
Enter fullscreen mode Exit fullscreen mode
Data Types Boolean Conversion
1 and 0 true and false
string and '' true and false
' ' true
null, undefined, and NaN false

Happy Coding!!!


Buy me a Coffee


TechStack Media | Bluehost

  • Get a website with a free domain name for 1st year and a free SSL certificate.
  • 1-click WordPress install and 24/7 support.
  • Starting at $3.95/month.
  • 30-Day Money-Back Guarantee.

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

Top comments (1)

Collapse
 
bello profile image
Bello Osagie

Thanks a lot.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay