educative.io

What is the purpose of converting a string or a number to a boolean?

I am curious if this has any applications and why the output is True.

const string = 'string';
let boolstring=Boolean(string); // true
console.log(boolstring);

Course: Complete JavaScript Course: Build a Real World App from Scratch - Learn Interactively
Lesson: Type Conversions - Complete JavaScript Course: Build a Real World App from Scratch

Hi, @max Thank you for contacting the Educative team with your query!

We convert a string or a number to a boolean for making sure that the string variable is empty or not. It will always return “true” if your variable has some value else it will return"false".

Please feel free to reply here if there is still some confusion.

Hope you have an amazing learning experience on Educative!

Hi @max, thanks for reaching out.
Actually, the pinned topic is based on type casting (which means how different types convert to each other). This piece of code explained how strings and numbers are converted to a boolean data type.

In short, a boolean is a variable that holds True if we have some non-zero value and False in case of having a value of zero.