bravokiloecho
bravokiloecho

Reputation: 1473

Querying a boolean variable versus comparing two strings in JS

In JavaScript, is there any significant difference in performance between querying a boolean variable's value or testing for the equality of two strings.

For example, which of these, if any, is more efficient:

Boolean example:

var testBoolean = false;

if (testBoolean) {
   alert('false')
}

String example:

var testString = 'false';

if (testString !== 'false') {
   alert('false')
}

Thanks!

Upvotes: 1

Views: 192

Answers (1)

user3991493
user3991493

Reputation:

var testString = 'false';

if (testString === false) {
   alert('I'm inside the if condition')
}

This will never go into the if condition because '===' and '!==' operator check both the value and the type unlike '==' and '!=' operators. But I think that is not your question.

Boolean is faster as you have one less comparison to make. You already have test set to false, but in second case you need to do string compare and push the result of that into the IF condition (which is the same as case 1 from here on)

Upvotes: 1

Related Questions