JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – boolean return value true with string or not

JavaScript Quiz – boolean return value true with string or not

boolean return value true with string

In javascript, boolean is a primitive value which means it doesn’t pass any reference to another variable. Booleans have only two values which are true and false which are not in the string format. And boolean have method called toString() & valueOf() which return a value of different data type.

We will use above this two method to manipulate the boolean value data type.

Basic Syntax of Boolean Method

toString()

valueOf()

For more detail about the Boolean and there method click here.

JavaScript Question

In this javascript quiz, did boolean return value true with string or not because boolean have method which modify data type of boolean.

First, we declare a variable boolean and assign a comparison value to it. Which return a boolean value.

let boolean = 10 > 9;

Now, we check the value of boolean variable and use toString() method with it.

console.log(boolean.toString());

Again, we check the same variable with valueOf() method.

console.log(boolean.valudOf());

Any idea, what should be the output of above code statement.

JavaScript Quiz Test

Here is the full code snipper for you to solve.

let boolean = 10 > 9;

console.log(boolean.valueOf());
// output => 🧐 ?

console.log(boolean.toString());
// output => 🧐 ?

// Made with ❤️, by @jsstartup

Answer – true, “true”

Click To Run Code

Explanation

Now, you know the answer is true, “true”. But why they look same than what’s the difference. Let me explain.

So, in short toString() method return a string value which mean it convert the boolean value to string.

And for valueOf() method, it return the primitive value which is actual value true.

At last, That’s why we get an answer true, “true.

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JavaScript Quiz – solve 2d array matrix diagonal addition
  2. JavaScript Quiz – find numbers difference with math abs
  3. JS Quiz – does get date and month from date object is same
  4. JS Quiz – does the string startsWith() method return true

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

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

Back to top
0 Shares
Share via
Copy link