The Language Flashcards
What is this scientific notation equal to?
2.998e8
2.998 x 10^8 = 299,800,000
What result do you get when you try to calculate any number of other numeric operations that don’t yield a precise, meaningful result.
NaN.
What are the characters you get when you press enter?
Newlines
How do you escape a character?
\
How do you create a newline?
\n
How do you create a tab character?
\t
What does the typeof operator do?
Typeof produces a string value naming the type of the value you give it.
Can strings be compared using booleans?
Yes. Ex: console.log(“Aardvark”
Are uppercase letters “less than” or “more than” lowercase letters?
uppercase letters are always “less” than lowercase ones, so “Z”
What value in Javascript is not equal to itself?
NaN. console.log(NaN == NaN) // -> false
NaN is supposed to denote the result of a nonsensical computation, and as such, it isn’t equal to the result of any other nonsensical computations.
When is the logical “and” operator true?
console.log(true && false) // → false console.log(true && true) // → true
It is a binary operator, and its result is true only if both the values given to it are true.
When is the logical “or” operator true?
It produces true if either of the values given to it is true.
console.log(false || true) // → true console.log(false || false) // → false
What kind of operator is the “not” operator and what does it do?
Not is written as an exclamation mark (!). It is a unary operator that flips the value given to it—!true produces false and !false gives true.
How does precedence work with standard boolean operators?
|| has the lowest precedence, then comes &&, then the comparison operators (>, ==, and so on), and then the rest.
What is the “conditional” operator also called and how does it work?
Sometimes just called the “ternary” operator since it is the only such operator in the language). The value on the left of the question mark “picks” which of the other two values will come out. When it is true, the middle value is chosen, and when it is false, the value on the right comes out.
console.log(true ? 1 : 2); // → 1 console.log(false ? 1 : 2); // → 2
What do the following expressions result in?
console. log(8 * null)
console. log(“5” - 1)
console. log(“5” + 1)
console. log(“five” * 2)
console. log(false == 0)
console.log(8 * null) // → 0 console.log("5" - 1) // → 4 console.log("5" + 1) // → 51 console.log("five" * 2) // → NaN console.log(false == 0) // → true
Define what “coercion” is. What’s another term for it?
Automatic type conversion. When an operator is applied to the “wrong” type of value, JavaScript will quietly convert that value to the type it wants, using a set of rules that often aren’t what you want or expect. This is called type coercion.
When is the last behavior below useful?
console.log(null == undefined); // → true console.log(null == 0); // → false
That last piece of behavior is often useful. When you want to test whether a value has a real value instead of null or undefined, you can simply compare it to null with the == (or !=) operator.
When converting strings and numbers to Boolean values, what 3 values count as false?
The rules for converting strings and numbers to Boolean values state that 0, NaN, and the empty string (“”) count as false, while all the other values count as true.
What is “short-circuit evaluation”?
In the case of true || X, no matter what X is—even if it’s an expression that does something terrible—the result will be true, and X is never evaluated. The same goes for false && X, which is false and will ignore X.
Define an “expression”
A fragment of code that produces a value is called an expression. Every value that is written literally (such as 22 or “psychoanalysis”) is an expression. An expression between parentheses is also an expression, as is a binary operator applied to two expressions or a unary operator applied to one.
What is the simplest kind of statement?
The simplest kind of statement is an expression with a semicolon after it. This is a program:
1;
!false;
When the = operator is used on existing variables, what does it do to the variable?
When a variable points at a value, that does not mean it is tied to that value forever. The = operator can be used at any time on existing variables to disconnect them from their current value and have them point to a new one.
var mood = "light"; console.log(mood); // → light mood = "dark"; console.log(mood); // → dark
What are other words used to mean that you are executing a function?
Executing a function is called invoking, calling, or applying it.
When are functions useful without “side effects”?
Showing a dialog box or writing text to the screen is a side effect. A lot of functions are useful because of the side effects they produce. Functions may also produce values, and in that case, they don’t need to have a side effect to be useful.
What kind of execution is an “if” statement?
Conditional execution.
Define a “block”
Whenever we need to execute multiple statements inside a loop, we wrap them in curly braces ({ and }). Braces do for statements what parentheses do for expressions: they group them together, making them count as a single statement. A sequence of statements wrapped in braces is called a block.
Write a program that calculates and shows the value of 2^10
var result = 1; var counter = 0; while (counter
What is the difference between and “do” and “while” loop?
The do loop is a control structure similar to the while loop. It differs only on one point: a do loop always executes its body at least once, and it starts testing whether it should stop only after that first execution. To reflect this, the test appears after the body of the loop:
do { var yourName = prompt("Who are you?"); } while (!yourName); console.log(yourName);
Using a for loop, write a program that calculates and shows the value of 2^10.
var result = 1; for (var counter = 0; counter
What does “breaking out of a loop” mean and how do you do it?
Having the loop’s condition produce false is not the only way a loop can finish. There is a special statement called break that has the effect of immediately jumping out of the enclosing loop.
This program illustrates the break statement. It finds the first number that is both greater than or equal to 20 and divisible by 7.
for (var current = 20; ; current++) { if (current % 7 == 0) break; } console.log(current); // → 21