This commit is contained in:
Ilya Kantor 2019-05-06 07:44:39 +02:00
parent 3623a881f3
commit db4d0de26d
3 changed files with 9 additions and 9 deletions

View file

@ -1,6 +1,6 @@
# Interaction: alert, prompt, confirm # Interaction: alert, prompt, confirm
This part of the tutorial aims to cover JavaScript "as is", without environment-specific tweaks. In this part of the tutorial we cover JavaScript language "as is", without environment-specific tweaks.
But we'll still be using the browser as our demo environment, so we should know at least a few of its user-interface functions. In this chapter, we'll get familiar with the browser functions `alert`, `prompt` and `confirm`. But we'll still be using the browser as our demo environment, so we should know at least a few of its user-interface functions. In this chapter, we'll get familiar with the browser functions `alert`, `prompt` and `confirm`.

View file

@ -2,7 +2,7 @@
Sometimes, we need to perform different actions based on different conditions. Sometimes, we need to perform different actions based on different conditions.
To do that, we use the `if` statement and the conditional (ternary) operator which we will be referring to as the “question mark” operator `?` for simplicity. To do that, we can use the `if` statement and the conditional operator `?`, that's also called a "question mark" operator.
## The "if" statement ## The "if" statement
@ -103,7 +103,7 @@ In the code above, JavaScript first checks `year < 2015`. If that is falsy, it g
There can be more `else if` blocks. The final `else` is optional. There can be more `else if` blocks. The final `else` is optional.
## Ternary operator '?' ## Conditional operator '?'
Sometimes, we need to assign a variable depending on a condition. Sometimes, we need to assign a variable depending on a condition.
@ -124,9 +124,9 @@ if (age > 18) {
alert(accessAllowed); alert(accessAllowed);
``` ```
The so-called "ternary" or "question mark" operator lets us do that in a shorter and simpler way. The so-called "conditional" or "question mark" operator lets us do that in a shorter and simpler way.
The operator is represented by a question mark `?`. The formal term "ternary" means that the operator has three operands. It is actually the one and only operator in JavaScript which has that many. The operator is represented by a question mark `?`. Sometimes it's called "ternary", because the operator has three operands. It is actually the one and only operator in JavaScript which has that many.
The syntax is: The syntax is:
```js ```js

View file

@ -84,7 +84,7 @@ The OR `||` operator does the following:
A value is returned in its original form, without the conversion. A value is returned in its original form, without the conversion.
In other words, a chain of OR `"||"` returns the first truthy value or the last one if no such value is found. In other words, a chain of OR `"||"` returns the first truthy value or the last one if no truthy value is found.
For instance: For instance:
@ -101,7 +101,7 @@ This leads to some interesting usage compared to a "pure, classical, boolean-onl
1. **Getting the first truthy value from a list of variables or expressions.** 1. **Getting the first truthy value from a list of variables or expressions.**
Imagine we have several variables which can either contain data or be `null/undefined`. How can we find the first one with data? Imagine we have a list of variables which can either contain data or be `null/undefined`. How can we find the first one with data?
We can use OR `||`: We can use OR `||`:
@ -143,7 +143,7 @@ This leads to some interesting usage compared to a "pure, classical, boolean-onl
alert(x); // 1 alert(x); // 1
``` ```
An assignment is a simple case. Other side effects can also be involved. An assignment is a simple case. There may be side effects, that won't show up if the evaluation doesn't reach them.
As we can see, such a use case is a "shorter way of doing `if`". The first operand is converted to boolean. If it's false, the second one is evaluated. As we can see, such a use case is a "shorter way of doing `if`". The first operand is converted to boolean. If it's false, the second one is evaluated.