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 Treehouse Club - MASH MASH - JavaScript Vocabulary Lesson

Question, Any Help Will Be Appreciated: what does this "||" And this "&&" Means?

Question, Any Help Will Be Appreciated: what does this "||" And this "&&" Means?

2 Answers

SRIMUGAN J
PLUS
SRIMUGAN J
Courses Plus Student 5,345 Points

Hi John, Those two are logical AND(&&) and logical OR operator(||). Those two are common in all programming languages.

What does it do? The answer is simple they are used with boolean logical values. for example let give a condition like these

if (condition1 && condition2){         
  executes some code here
}

In the above sample the condition will execute only if both the condition are true if anyone of the condition fails the code not executed. Let go for an small example for these

var question = prompt("Enter your Age");
if(question>12&&question<20){
alert("you are teenager")
}
else{
 alert("you are not teenager")
}

Let break the code line by line first prompt box is created and assigned it to the variable name question, when you run the code the prompt dialog box appers. If you entered the values between 13 to 19 the alert box 'll show you are teenager otherwise the condition fails else part executes. Note the above code written was in javascript but the logical and operator and or operator remains same in the all of the programming language.

Like wise the OR operator but the difference is it executes the code either one of the conditions become true. You can try the OR operator on your own.

If you are learning the Javascript refer this link for more details https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Expressions_and_Operators#Logical_operators.

Hope this helps,

Huzaifa Sajjad Malik
PLUS
Huzaifa Sajjad Malik
Courses Plus Student 10,823 Points

||(OR) is a logical operator which will result true if any one of the condition is true.Say for example

var color=prompt("Enter a color");
if(color==="black" || color==="blue")
{
   //code here would be executed if either the color would be black or blue
}
// &&(AND) is a logical operator which will result true if both the conditions are true.Say for example
var font_size=15;
if(color==="black" && font_size===15)
{
   //code here would be executed if the color would be black and the model number would be equal to 15.
}