Skip to Main Content
JavaScript and Open Data
book

JavaScript and Open Data

by Robert Jeansoulin
August 2018
Intermediate to advanced content levelIntermediate to advanced
272 pages
5h 3m
English
Wiley-ISTE
Content preview from JavaScript and Open Data

2Controls: Booleans, Branch and Loops

2.1. Truth values and boolean operators

A control is a boolean context that is defined by the syntax of a conditional instruction:

  • – branch: if(exp. in boolean context) {evaluated true} else {evaluated false};
  • – loop: while (exp in boolean context) {repeat block of code}.

As is no surprise, there are two boolean values: true and false. What is more specific is that, in a boolean context, the values of type string or number are cast to a boolean, and values undefined and null are evaluated as false.

2.1.1. Boolean operators: “!” (not), “&&” (and), “ ||” (or)

Allowing to build logical expressions in classical boolean logics, we get:

let p = true, q = false;
 console.log( typeof p);      // -> boolean
 console.log( p || q);              // -> disjonction -> true
 console.log( !p || q);       // -> logical imply: p => q () -> false

NOTE.– Evaluation from left to right, priority to inner parentheses, short-cut rules.

let bool1 = true, bool2 = false;
let p = bool1 || bool2; // bool2 not evaluated: p already 'true'
let q = bool2 && bool1; // bool1 not evaluated; q already 'false'

2.1.2. Relational operators: >, <, >=, <=

With numbers, the relation is the order relation in ℜ (real numbers); with strings, it is the Unicode order relation where figures [0-9] precede alphas.

let x = 4; console.log(x>3); console.log(x>'3'); // 'true' 'true'
console.log('b' > 'a'); console.log('b' > '3'); // 'true' 'true'
// warning! figures are like characters, hence:
console.log( ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Data Wrangling with JavaScript

Data Wrangling with JavaScript

Ashley Davis
Web Applications with Javascript or Java

Web Applications with Javascript or Java

Gerd Wagner, Mircea Diaconescu

Publisher Resources

ISBN: 9781786302045