Introduction to Web Development Introduction to Web Development

JavaScript Logical Operators and Arrays

This course has been updated! We now recommend you take the Complete Intro to Web Development, v2 course.

Check out a free preview of the full Introduction to Web Development course:
The "JavaScript Logical Operators and Arrays" Lesson is part of the full, Introduction to Web Development course featured in this preview video. Here's what you'd learn in this lesson:

Logical operators or often used to create complex logical expressions. For example, “If a equals b and x equals y”. JavaScript has many logical operators include && (and), || (or), and !(not). Nina demonstrate a few logic expressions and also introduces JavaScript Arrays.

Get Unlimited Access Now

Transcript from the "JavaScript Logical Operators and Arrays" Lesson

[00:00:00]
>> [MUSIC]

[00:00:04]
>> Nina Zakharenko: So here are some thing that's kind of tricky. JavaScript supports logical operators. I'm not gonna do truth tables right now, so I'll just show you guys how to use these logical operators in the console. So, I'm just gonna put a comma here so it doesn't execute. This symbol means AND.

[00:00:28] And AND means that you have two values, an A and a B. For AND to return true, both those values have to be true. So if I do true and true.
>> Nina Zakharenko: We're gonna get a true. I do a true and a false, we're gonna get a false.

[00:00:54]
>> Brian Holt: You need to [INAUDIBLE].
>> Nina Zakharenko: Yes. False and false is false. True and false is false. False and true also false so there's only one condition where AND will return true. The second guy we have here is these two pipe-looking thingies, and that's an OR operation. So OR means that either one of the values is true, and a fun thing about OR is if the first value is true, JavaScript is like okay, cool I'm done.

[00:01:31] It doesn't matter what the second value is. So you can do like true and blah, blah, blah. Oops, true and-
>> Brian Holt: Two [INAUDIBLE].
>> Nina Zakharenko: Thank you, sorry guys. Sorry, I'm using the wrong symbol and that's why it's not working. So true or whatever, doesn't matter if it's defined or not or like not even valid JavaScript.

[00:02:11] JavaScript is like, I got a true, cool, I'm done. So true or false, true, false or true, also true
>> Nina Zakharenko: The only time OR returns false if is if both values are false. So NOT is just a negation. Not true is false, not false is true. And we can combine these, the AND and the OR and the NOT in different combinations.

[00:02:47] I won't go into anything too complicated, but for an example. Let's do true and true. We're gonna use parenthesis here, and we're gonna negate the value of what's inside those parenthesis. So true and true returns true. And then negating it returns false.
>> Nina Zakharenko: Is anyone have questions about this?

[00:03:15]
>> Speaker 3: There's question about the single logical operators. Do they perform bit-wise operations?
>> Nina Zakharenko: Yeah, but not something you wanna mess with in JavaScript unless you're crazy, pretty much.
>> Brian Holt: Even if you're crazy.
>> Nina Zakharenko: Yeah, just don't do it.
>> Nina Zakharenko: So, here's a combination of a logical operator and a comparison.

[00:03:43] So we have x is 5 and y is 3. We check, is x less than 4? The value is false. Is y less than 3? Yeah it is, so false and true is false.
>> Nina Zakharenko: Oops, same with this operation here at the bottom. X is not less than 4.

[00:04:06] But y is less than 4. So if we do that expression and we OR it we'll get back a true. So one of the most useful features of JavaScript are arrays. So arrays are a list of variables. They're written with square brackets, so here's a notation for an array.

[00:04:33] The values in it are separated with a comma. Arrays have some useful properties to them. One of them being length. So, oops, if we make an array here
>> Nina Zakharenko: We use an open score bracket to start it we put in some strings and they're common delimited. We use closing bracket to close it.

[00:05:11] So now we have our array. We use the name of our array, we can call .link on it. It'll give back at 2. So if you're using the Chrome JavaScript console, just a helpful thing to know is, it knows what methods are available on this, so if you start typing, you'll get some helpful hints there.

[00:05:49]
>> Nina Zakharenko: So, if we wanna access an item in our array, we use square brackets. So we call the name of our array, open bracket, a number, close bracket. So an important thing to note is that array access is actually zero based. So even though the length of our array is two, we would access the first item by callings fruits zero.

[00:06:13] And the second item by calling fruits one