JS Basics. Flashcards

1
Q

What are comments?

A

1.) By default, each line of text in the source files of a program is considered a statement that should be executed. You can prevent certain lines from executing by putting a double slash before them: //. This turns the code into a comment.
(e.g)
// console.log(“Let’s do some math”);
console.log(4 + 7);
2.) During execution, the commented-out lines no longer produce results. As we hoped, they weren’t executed.
3.) You can also write comments by typing /* / around the code you want commented out.
(e.g)
/
A comment
written on
several lines */

4.) Adding comments to complicated or critical parts is a good habit you should build right now!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you display information while writing JS?

A

Both console.log() and alert() can be used to display information to the user. Unlike alert(), console.log() does not stop program execution and is often a better choice.

console.log() can also display several comma-separated values at once.
(e.g)
const temp1 = 36.9;
const temp2 = 37.6;
const temp3 = 37.1;
console.log(temp1, temp2, temp3); // Show “36.9 37.6 37.1”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the modern way of writing JS?

A

1.) The directive looks like a string: “use strict” or ‘use strict’. When it is located at the top of a script, the whole script works the “modern” way.

For example:

“use strict”;

// this code works the modern way

2.) Please make sure that “use strict” is at the top of your scripts, otherwise strict mode may not be enabled.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a Modal Window?

A

The mini-window which appears when a couple of functions to interact with the user: alert, prompt and confirm are run in the console.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does JS interact with users on the browser?

A

1.) alert
shows a message.
2.) prompt
shows a message asking the user to input text. It returns the text or, if Cancel button or Esc is clicked, null.
3.) confirm
shows a message and waits for the user to press “OK” or “Cancel”. It returns true for OK and false for Cancel/Esc.

All these methods are modal: they pause script execution and don’t allow the visitor to interact with the rest of the page until the window has been dismissed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are || is Type Conversion?

A

1.) Most of the time, operators and functions automatically convert the values given to them to the right type.
2.) There are also cases when we need to explicitly convert a value to the expected type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the main areas of string conversion?

A

1.) String Conversion.
String conversion is mostly obvious. A false becomes “false”, null becomes “null”, etc
let value = true;
alert(typeof value); // boolean

value = String(value); // now value is a string “true”
alert(typeof value); // string
2.) Numeric Conversions
3.) Boolean Conversion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do Numeric Conversions work?

A

Numeric Conversion
Numeric conversion happens in mathematical functions and expressions automatically or We can use the Number(value) function to explicitly convert a value to a number:
Please note that null and undefined behave differently here: null becomes zero while undefined becomes NaN.
(e.g)
let str = “123”;
alert(typeof str); // string

let num = Number(str); // becomes a number 123

alert(typeof num); // number
alert( Number(“ 123 “) ); // 123
alert( Number(“123z”) ); // NaN (error reading a number at “z”)
alert( Number(true) ); // 1
alert( Number(false) ); // 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do Boolean Conversions work?

A

1.) It happens in logical operations. but can also be performed explicitly with a call to Boolean(value).
2.)
Values that are intuitively “empty”, like 0,
an empty string,
null, undefined, and NaN,
become false.
Other values become true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do Comparisons work in JS?

A

1.) Comparison operators return a boolean value.
2.) Strings are compared letter-by-letter in the “dictionary” order.
3.) When values of different types are compared, they get converted to numbers (with the exclusion of a strict equality check).
4.) The values null and undefined equal == each other and do not equal any other value.
5.) Be careful when using comparisons like > or < with variables that can occasionally be null/undefined. Checking for null/undefined separately is a good idea.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are Logical Operators?

A

There are four logical operators in JavaScript: || (OR), && (AND), ! (NOT), ?? (Nullish Coalescing).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does OR || do?

A

1.) Evaluates operands from left to right.
For each operand, converts it to boolean.
2.) If the result is true, stops and returns the original value of that operand.
3.) If all operands have been evaluated (i.e. all were false), returns the last operand.

console.log(null || “user”)
// → user
console.log(“Agnes” || “user”)
// → Agnes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a Nullish Coalescing Operator ‘??’

A

1.) This is a recent addition to the language.
2.) Written as two question marks ??.
3.) The result of a ?? b is:

if a is defined, then a,
if a isn’t defined, then b.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Whats the difference between ?? and || ?

A

1.) || returns the first truthy value.
2.) ?? returns the first defined value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Who has Precedence?

A

1.) The precedence of the ?? operator is the same as ||. They both equal 3 in the MDN table.

2.) That means that, just like ||, the nullish coalescing operator ?? is evaluated before = and ?, but after most other operations, such as +, *.
3.) Due to safety reasons, JavaScript forbids using ?? together with && and || operators, unless the precedence is explicitly specified with parentheses.

4.) 1 + 1 == 2 && 10 * 10 > 50

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are Loops?

A

Loops are a way to repeat the same code multiple times.

17
Q

What is a While Loop ?

A

while (condition) {
// code
// so-called “loop body”
}

18
Q

What is Iteration?

A

A single execution of the loop body is called an iteration.

19
Q

What is the “do…while” loop?

A

1.) The condition check can be moved below the loop body using the do..while syntax:

do {
// loop body
} while (condition);

3.) This form of syntax should only be used when you want the body of the loop to execute at least once regardless of the condition being truthy. Usually, the other form is preferred: while(…) {…}.

20
Q

What is an Inline Variable Declaration?

A

Here, the “counter” variable i is declared right in the loop. This is called an “inline” variable declaration. Such variables are visible only inside the loop.

for (let i = 0; i < 3; i++) {
alert(i); // 0, 1, 2
}
alert(i); // error, no such variable

21
Q

How to Break the loop…!

A

Normally, a loop exits when its condition becomes falsy.

1.) But we can force the exit at any time using the special break directive.
2.) The combination “infinite loop + break as needed” is great for situations when a loop’s condition must be checked not in the beginning or end of the loop, but in the middle or even in several places of its body.

22
Q

What is a Unary Operator?

A

Unary operators

Not all operators are symbols. Some are written as words. One example is the typeof operator, which produces a string value naming the type of the value you give it.

console.log(typeof 4.5)
// → number

2.) 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.

23
Q

What is a Ternary Operator?

A

Operating on three values. It is written with a question mark and a colon, like this:

console.log(true ? 1 : 2);
// → 1
console.log(false ? 1 : 2);
// → 2