DEV Community

Simon Mannes
Simon Mannes

Posted on • Originally published at mannes.tech on

1 1

3 Quick Tips to Convert Types In JavaScript

JavaScript has 6 primitive data types.

They are string, number, boolean, null, undefined, and symbol.

What's a symbol you ask? ECMAScript 2015 introduced them. They are a way to create globally unique values/identifiers with descriptions. This article does a great job explaining them.

And then there are objects. We will talk about them in another article.

Here are 3 quick tips for converting data to one specific primitive:

  • Boolean conversion. All JS values are truthy, except "", null, undefined, NaN, 0, and false. You can explicitly convert values to a boolean by using !!. !!0 === false && !!NaN === false && !!"" === false.
  • String conversion. Convert any primitive value to a string by adding an empty string. null + "" === "null". Since ES6 you can also use template strings for this: `${null}` === "null".
  • Number conversion. The primitive values string, null, and boolean can be converted to numbers with +. +null === 0 && +true === 1 && +false === 0 && +'0' === 0 && +'100' === 100.

You can also use the global methods String(), Number(), and Boolean().
They make your conversion explicit and readable.

Follow me on Twitter @simon_mannes

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more