Lessons: 65Length: 7.1 hours

Next lesson playing in 5 seconds

Cancel
  • 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.Introduction
2 lessons, 07:42

1.1
Introduction
02:12

1.2
Setup
05:30

2.Language Fundamentals
8 lessons, 1:00:53

2.1
Variables
06:33

2.2
Data Types
11:28

2.3
Arithmetic, Assignment, and Comparison Operators
10:24

2.4
Unary, Logical, Comma, and Spread Operators
09:02

2.5
Operator Precedence
03:50

2.6
Reserved Words
04:17

2.7
Strict Mode
04:34

2.8
Functions
10:45

3.Data Structures
5 lessons, 22:52

3.1
Arrays
04:29

3.2
Objects
04:30

3.3
Sets
04:57

3.4
Maps
04:21

3.5
Weak Maps and Weak Sets
04:35

4.Controlling Program Execution
7 lessons, 37:06

4.1
Conditionals
07:49

4.2
Switch Statements
04:41

4.3
The For Loop
06:39

4.4
The `for .. in` Loop
05:17

4.5
The `for .. of` Loop
04:02

4.6
Iterators
05:03

4.7
While Loops
03:35

5.Using JavaScript
13 lessons, 1:44:36

5.1
Working With Strings
09:32

5.2
Template Literals
05:46

5.3
Working With Numbers
06:57

5.4
Working With Arrays
12:53

5.5
Iterating and Transforming Arrays
07:33

5.6
Working With the Object Type
13:55

5.7
Object Literal Extensions
06:45

5.8
Working With Object Instances
06:45

5.9
Getters and Setters
05:00

5.10
Custom Objects
11:28

5.11
The `Math` API
04:54

5.12
Working With Dates and Times
08:10

5.13
The `Array` Constructor
04:58

6.Functions
8 lessons, 56:07

6.1
The `this` Object
06:15

6.2
Working With Functions
10:11

6.3
Scope
07:37

6.4
Arrow Functions
06:59

6.5
Generator Functions
08:13

6.6
Closures
05:00

6.7
Prototypes
06:26

6.8
Default and Rest Parameters
05:26

7.Miscellaneous
6 lessons, 52:39

7.1
Destructuring Assignments
08:09

7.2
AJAX
08:30

7.3
Regular Expressions
10:51

7.4
More About Regular Expressions
08:38

7.5
Classes
06:48

7.6
ES Modules
09:43

8.Working With the DOM
6 lessons, 37:39

8.1
Selecting HTML Elements
05:02

8.2
Manipulating HTML Elements
07:40

8.3
DOM Traversal
05:25

8.4
Adding and Removing Elements
04:45

8.5
Creating Elements and Other Nodes
04:39

8.6
DOM Events
10:08

9.Web APIs
4 lessons, 17:41

9.1
The Selector API
03:03

9.2
Geolocation
05:29

9.3
Web Storage
05:24

9.4
Web Workers
03:45

10.Asynchronous JavaScript
5 lessons, 26:23

10.1
Promises
09:52

10.2
Promise Chaining
05:11

10.3
The async Keyword
03:21

10.4
The await Keyword
04:04

10.5
More About async and await
03:55

11.Conclusion
1 lesson, 00:43

11.1
Conclusion
00: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.

Back to the top