Learn JavaScript in Construct, part 3: operators

36

Index

Features on these Courses

Stats

12,752 visits, 30,024 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY-NC 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 1 Oct, 2021. Last updated 16 Dec, 2021

Converting to boolean

Strings and numbers can both also be converted to booleans, reducing them to either a true or false value. Similar to the other types, this can be done with Boolean().

JavaScript has lots of rules for how to convert various different data types and special values to a boolean. For now though we'll focus on just the following cases.

  • With numbers, 0 and the special value NaN convert to false, and anything else converts to true.
  • With strings, an empty string "" converts to false, and anything else converts to true.
  • The special value undefined converts to false.

See the following examples.

// Try entering in to the console:
Boolean(0)			// false (is zero)
Boolean(NaN)		// false (is NaN)
Boolean(1)			// true (is not zero or NaN)
Boolean(42)			// true (is not zero or NaN)
Boolean(Infinity)	// true (is not zero or NaN)

Boolean("")			// false (is empty string)
Boolean("Hello")	// true (is not empty string)
Boolean("0")		// true (is not empty string)

Boolean(undefined)	// false

Boolean(true)		// true (no change)
Boolean(false)		// false (no change)

Collectively, all values that convert to true are referred to as truthy values, and all values that convert to false are referred to as falsy values. For example the number 5 is truthy, and an empty string is falsy.

Note that the rules for truthy and falsy depend on the type. The number 0 is falsy because it equals zero, but a string with the content "0" is truthy, because it is not an empty string.

Logical operators

There are some operators that are especially useful to use with booleans.

Not

You can invert a boolean - making it turn from true to false or vice versa - with the not operator !, which must go before the boolean, e.g. !b.

let b = true;
console.log(!b);  // false

OR/AND

Suppose you want to know if either of two conditions are true, or if both are true. The logical operators OR || and AND && can identify this.

For example a < b || a < c will return true if a is less than b or if a is less than c; otherwise it returns false. Similarly a < b && a < c will return true if a is less than b and if a is less than c; otherwise it returns false.

You can use the console to test all combinations of comparisons with the logical operators:

// Try entering in to the console:
false || false		// false
false || true		// true
true || false		// true
true || true		// true

false && false		// false
false && true		// false
true && false		// false
true && true		// true

These operators also work with any truthy or falsy values, and the result will also be truthy or falsy the same way it works with true and false. For example 0 || 5 returns 5 (the truthy value), and "Hello" && "" returns "" (the falsy value). This might seem odd, but it does come in useful when using things like 'if' statements later on.

Conditional ?: operator

The only ternary operator - an operator that involves three values - in Javascript is the conditional ?: operator. This takes the form comparison ? valueIfTruthy : valueIfFalsy. For example the following code snippet will log Nice score! because the score variable of 120 is greater than or equal to 100.

let score = 120;
console.log(score >= 100 ? "Nice score!" : "Try again!");

Try changing score to initialise to 50. Notice the text logged to the console changes to Try again!, because the condition score >= 100 has become false, so the last value is returned.

JavaScript often accepts any kind of truthy or falsy value where a comparison is made, and this applies here as well. The first comparison value can be a different type like a number or a string, and it will test whether the value is truthy or falsy.

// Try entering in to the console:
0 ? "truthy" : "falsy"			// "falsy"
1 ? "truthy" : "falsy"			// "truthy"
"" ? "truthy" : "falsy"			// "falsy"
"Hello!" ? "truthy" : "falsy"	// "truthy"

Once again, this will come in useful when we come to use 'if' statements.

Conclusion

In this part we've covered:

  • The mathematical operators % and **
  • Additional assignment operators like += and *=
  • Increment and decrement operators ++ and --
  • Building strings with both + and template literals
  • Converting between types, including between numbers and strings, as well as to booleans and the concept of truthy and falsy values
  • Comparison operators like < and ===
  • Logical operators !, ||, &&
  • The conditional ?: operator

In the next part we'll begin to use control flow, such as the 'if' statement we've alluded to a couple of times by now. This is where you can start to write programs that change what they do depending on conditions - such as a "guess the number" game!

Learn more

If you want to dig deeper, you can learn more about the features mentioned in this guide at the following MDN Web Docs links:

Part 4

When you're ready to continue, head on to the next part at Learn JavaScript in Construct, part 4: control flow!

  • 4 Comments

  • Order by
Want to leave a comment? Login or Register an account!