Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Review Else If Clauses and JavaScript Comments

Miguel Nunez
Miguel Nunez
3,266 Points

What is a And operator and a or operator in Java-script?

I want simplicity to this. Now i'm getting lost now what are the functions and purpose of a and operator and a or operator?

Miguel Nunez , AND(&&) or OR(||) both are logical operators and have different functionality.Let take an example suppose you want to execute code particular code if course is javascript and points not more than 500.

  //AND operator
if(course=="javascript" && points<=500){
  //code will execute when both condition evaluates true
}

//OR operator
if(course=="javascript" || points<=500){
  //code will execute when a single condition evaluates to true
}

AND = TRUE (All conditions are true) OR = TRUE(One conditions evaluates to true )

Miguel Nunez
Miguel Nunez
3,266 Points

The part i'm getting confused is what are we comparing in the and section in other words what is the key word course representing I know where using a boolean stuff here but what is course representing. since were using this symbol == to show equal

2 Answers

Ante Adamović
seal-mask
.a{fill-rule:evenodd;}techdegree
Ante Adamović
Front End Web Development Techdegree Student 2,508 Points
var a = 1;
var b = 2;
if(a == b) 
// will returns false, because we're comparing values of 
// variables (a) and (b) which are (1) and (2), is 1 equal to 2 ? no
// lets add variable c = 2
var c = 2;
if(a == c) // false -> is value of (a) equal to value of (c) -> is 1 equal to 2 ? no
if(b == c) // true -> is value of (b) equal to value of (c) -> is 2 equal to 2 ? yes

// Now lets see how && and || work here
if( a == b && a == c) 
// it returns false, why ?
// first it checks (a == b) which we know is false (use above as reference),
// since we're using && where BOTH conditions need to be true for the result
// to be true, it's automatically false
if( a == b || a == c)
// it returns true, why ?
// first it checks (a == b) which we know is false (use above as reference),
// since we're using || where atleast ONE of the conditions needs to be true for 
// the result to be true it procedes to check second condition
// (a ==c) is true, use above as reference, therefore our IF statement looks like
// if( false || true) -> results in true
if ( a != b && a == c) 
// results in true, why ?
// first condition (a!=b) is (a) not equal to (b), true
// second condition (a==c) is (a) equal to (c), true
if ( true && true ) -> true

To explain it as simple as possible, when using logical operators, if && is used then BOTH sides of the statement need to be true:

a && b will result in true ONLY if (a = true) and (b = true)

if || is used then EITHER side of the statement need to be true:

a || b will result in true EITHER if (a = true, b = false), (a = false, b = true) or (a = true, b = true)

I advise you make 4 boolean variables such as (var a = true, b = true, na = false, nb = false) and play around with them to see how they behave.

Miguel Nunez
Miguel Nunez
3,266 Points

The part i'm getting confused is what are we comparing in the and section in other words what is the key word course representing I know where using a boolean stuff here but what is course representing. since were using this symbol == to show equal

ysantana
ysantana
9,579 Points

Miguel Nunez. The || and && symbols are logical operators, and there are used to determine the logic between variables and values. There are mostly used with Boolean values. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

The == symbol is a comparison operator with type coercion allowed(if the operands of the operator are different types, one of them will be converted to an "equivalent" value of the other operand's type) and then checks whether the conditions evaluate to True or False. The == operator only checks if the conditions have equal value. On the other hand, The === operator, does not allowed type coercion, and checks for value and type equality.

Review this sample codes for better understanding:

For Example:

// && operator example
var test1 =  true && true;     // t && t returns true
var test2 =  true && false;    // t && f returns false
var test3 = false && true;     // f && t returns false
var condition = false && (8 == 4);  // f && f returns false

// || operator example
var test4 =  true || true;     // t || t returns true
var test5 = false || true;     // f || t returns true
var test6 =  true || false;    // t || f returns true
var test7 = false || (6 == 3); // f || f returns false
Miguel Nunez
Miguel Nunez
3,266 Points

Hey Yeramin Santana Correct me if I'm wrong but an operator in javascript is a symbol (>,>=,<,<=,==,===,!=,!==) that is used to perform an operation correct? and It becomes false right if they are not right in logic to each other? and a and (&&) is when two elements are true in comparison ? and a or is when at least one of the elements is true with two elements being in comparison ? I feel like i'm lacking understanding or perhaps I want to make sure the way i'm understanding this is accurate.