- Overview
- Transcript
2.4 Unary, Logical, Comma, and Spread Operators
In this lesson we’ll continue looking at JavaScript’s many operators. This time we’ll focus on unary operators, logical operators, the comma operator, and the spread operator.
Syntax
delete
typeof
void
&&
||
!
,
...
1.Introduction2 lessons, 07:42
1.1Introduction02:12
1.2Setup05:30
2.Language Fundamentals8 lessons, 1:00:53
2.1Variables06:33
2.2Data Types11:28
2.3Arithmetic, Assignment, and Comparison Operators10:24
2.4Unary, Logical, Comma, and Spread Operators09:02
2.5Operator Precedence03:50
2.6Reserved Words04:17
2.7Strict Mode04:34
2.8Functions10:45
3.Data Structures5 lessons, 22:52
3.1Arrays04:29
3.2Objects04:30
3.3Sets04:57
3.4Maps04:21
3.5Weak Maps and Weak Sets04:35
4.Controlling Program Execution7 lessons, 37:06
4.1Conditionals07:49
4.2Switch Statements04:41
4.3The For Loop06:39
4.4The `for .. in` Loop05:17
4.5The `for .. of` Loop04:02
4.6Iterators05:03
4.7While Loops03:35
5.Using JavaScript13 lessons, 1:44:36
5.1Working With Strings09:32
5.2Template Literals05:46
5.3Working With Numbers06:57
5.4Working With Arrays12:53
5.5Iterating and Transforming Arrays07:33
5.6Working With the Object Type13:55
5.7Object Literal Extensions06:45
5.8Working With Object Instances06:45
5.9Getters and Setters05:00
5.10Custom Objects11:28
5.11The `Math` API04:54
5.12Working With Dates and Times08:10
5.13The `Array` Constructor04:58
6.Functions8 lessons, 56:07
6.1The `this` Object06:15
6.2Working With Functions10:11
6.3Scope07:37
6.4Arrow Functions06:59
6.5Generator Functions08:13
6.6Closures05:00
6.7Prototypes06:26
6.8Default and Rest Parameters05:26
7.Miscellaneous6 lessons, 52:39
7.1Destructuring Assignments08:09
7.2AJAX08:30
7.3Regular Expressions10:51
7.4More About Regular Expressions08:38
7.5Classes06:48
7.6ES Modules09:43
8.Working With the DOM6 lessons, 37:39
8.1Selecting HTML Elements05:02
8.2Manipulating HTML Elements07:40
8.3DOM Traversal05:25
8.4Adding and Removing Elements04:45
8.5Creating Elements and Other Nodes04:39
8.6DOM Events10:08
9.Web APIs4 lessons, 17:41
9.1The Selector API03:03
9.2Geolocation05:29
9.3Web Storage05:24
9.4Web Workers03:45
10.Asynchronous JavaScript5 lessons, 26:23
10.1Promises09:52
10.2Promise Chaining05:11
10.3The async Keyword03:21
10.4The await Keyword04:04
10.5More About async and await03:55
11.Conclusion1 lesson, 00:43
11.1Conclusion00:43
2.4 Unary, Logical, Comma, and Spread Operators
Hi folks. In this lesson we're going to continue looking at Operators but this time move on to some slightly more advanced ones than the ones we looked at in the last lesson. We've a number of them to look at so let's make a start with the unary operators which are operators which only have a single operand rather than two. The delete operator is used to delete an object property. So we've declared a simple object In the variable test and this object has a single key which is x and this key contains the value x in string form. So let's just take a look at this in the browsers console. And we can see that it's load to string x to the console. So now, before we do the console log, let's use the delete operator to delete the x key. And now, if we go back to the browser, we can see that it has now logged the value undefined, because x no longer exists in the test object. So the delete operator deletes a property from an object. It only works in this situation. We can’t use delete to delete a variable. Another unary operator is type of which we can sometimes use to determine the type of a value. And let's just take a look at the output of that in the browser. And we can see that the type of value that the word variable contains is a string. The typeof operator works well with primitives, but it does not work quite so well with complex types. Let's just look at another basic example. Before we go back to the browser, just have a little think about what you think the browser console is going to display. Even though we declared an array, the type of operator reports that it is an object. Now technically, most things in JavaScript are objects. But because the array is a special type of object, it would be useful if typeof reported array as being an array, instead of an object. However, that's how it works. There are ways that we can use to tell whether the object we're working with is an array or an object, and we'll comeback and look at that later in the course. For now just be aware that type of may not report the true type depending on the value that we're using it with. The final unary operator is the void operator. It's more often used in TypeScript than it is in JavaScript, and in fact it's rarely used these days in JavaScript at all. It used to be used frequently with anchor tags when we didn't want the browser to try and follow the anchor. If we just go to the index.html page briefly, it used to be very common to see anchor tags like this. Whenever somebody clicked the anchor tag, instead of the browser trying to redirect the user to some of the resource in the Internet, it would instead do nothing at all. That was the most common use for the void operator in regular JavaScript but not as common these days. So, you probably won't ever write that yourself. You might not even come across that in the world. But it does exist so it’s worth pointing out. Another type of operator in JavaScript is the logical operators. Let’s take a look at these starting with the andOperator. The logical andOperator, is two ampersands, one directly after the other. This operator returns true only of both of the operands are true, such as in this case. If either one or both of the operands evaluates to false, it returns false instead. There is also the orOperator and this is denoted with a double pipe symbol. This time the orOperator returns true if any of the operands evaluate to true. So in this case, only the last expression returns false, because all of the others have at least one true operand. So the all operator is basically the diametric opposite that the andOperator. The last logical operator is the NOT operator and this is denoted with an exclamation mark. The NOT operator basically negates the value of the operand it is used with. So in this case the value true becomes the value false. Like the unary operators, the NOT operator is used with a single operand. Another operator is the comma operator and it is used to include multiple expressions in a place where only one expression is expected. It evaluates each of its operands and returns the value of the last operand. So this expression in which two variables are declared both variables will get created and have the specified values assigned to them but only the last variable would be returned. The comma operator should not be confused with this simple comma separator. So, the comma separator is used in a number of places in JavaScript, like if we are declaring an object with multiple keys like this. So the comma within this object is a separator and not the comma operator. The comma operator is actually very rarely used. You'll most often see the comma used as a separator. The spread operator is the newest operator in JavaScript and is denoted by three dots together in a line. The spread operator also uses a single operand and the operand always comes to the right of the operator. This operator is nearly always used with arrays or functions, and we haven't really looked to either of these things yet. So there isn't really a worthwhile example that I can give you at this point in the course. We will come back to it and look at the spread operator in more detail later in the course. For now it's enough to know that it exists. In addition to the operators that we've looked at in this and the previous lesson, there is another whole group of operators called bit wise operators. We're not going to look at these here because they are rarely used and they are quite technical in nature. I'd recommend that you wait until you're comfortable with more of the basics of the language. Then as an exercise in self improvement you can look them up and study them at your own leisure. So in this lesson we looked at some slightly more advanced operators than we looked at in the last lesson. We covered the unary operators which work only with a single operand and these are delete, type of, and void, and the logical operators, and, or, and not. We also looked briefly at the comma and spread operators. Together with the operators from the last lesson, this covers the majority of the available operators in JavaScript, and certainly covers the most frequently used. In the next lesson, we can look at Operator Precedence or the order in which operators are evaluated when multiple operators appear in the same expression. Thanks for watching.