Skip to Content
Learning JavaScript
book

Learning JavaScript

by Shelley Powers
October 2006
Beginner to intermediate
352 pages
9h 33m
English
O'Reilly Media, Inc.
Content preview from Learning JavaScript

The Logical Operators

Most of the examples so far in the book show a conditional expression that consists usually of one operator and two operands, such as the following:

if (sValue == 'test')

However, many times a conditional expression is dependent on several different conditions being met, each represented by an expression and combined through the use of one of JavaScript’s logical operators.

There are three logical operators—two binary and one unary. The first is the logical AND, represented by two ampersand characters, &&. When used in a conditional statement, the AND operator requires that expressions on both sides of the operator evaluate to true for the entire expression to evaluate to true:

var nValue = 10;
if ((nValue > 10) && (nValue <=100)) // true if nValue is greater than 10 and nValue is less than or equal to 100

The result of using this expression joined by the AND operator is false because the variable, nValue, is equal to 10, which means the first expression is false. If the first expression evaluates to false, the JavaScript engine won’t process the second expression because the entire statement is going to fail regardless.

The second operator is the logical OR operator, represented by two vertical lines, ||. When used in a conditional statement, the OR operator requires one or the other of its expressions on either side to be true in order for the entire expression to evaluate to true:

var nValue = 10; if ((nValue > 10) || (nValue <= 100)) // true if nValue is either ...
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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning JavaScript, 2nd Edition

Learning JavaScript, 2nd Edition

Shelley Powers

Publisher Resources

ISBN: 0596527462Catalog PageErrata