JavaScript Essentials Flashcards

Learn specifics of the vanilla implementation of JavaScript

1
Q

Event executed when the browser fully loaded HTML, and the DOM tree is built, but external resources like pictures <img></img> and stylesheets may be not yet loaded.

A

document.addEventListener(“DOMContentLoaded”, function() { … });

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

Event executed when not only HTML is loaded, but also all the external resources: images, styles etc.

A

window.onload = function() { … }

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

JavaScript programs can be inserted into any part of an HTML document. True or false?

A

True

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

A single tag can’t have both the src attribute and code inside (If src is set, the script content is ignored). True or false?

A

True

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

The type and language attributes are required in a tag. True or false?

A

False

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

Will this code run correctly?:

alert(“There will be an error”)

[1, 2].forEach(alert)

A

No.

The error occurs because JavaScript does not assume a semicolon before square brackets […].

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

Nested comments are supported (there may be // inside another //). True or False

A

False

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

The directive to set strict-mode looks like a string: “use strict” or ‘use strict’. True or False

A

True

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

The “use strict” directive switches the engine to the “modern” mode, changing the behavior of some built-in features. True or False

A

True

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

What are the naming rules for variables in JS?

A

The name must contain only letters, digits, or the symbols $ and _.
The first character must not be a digit.

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

Variables are case sensitive. True or false?

A

True

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

Are non-Latin letters are allowed in variables?

A

Yes, but they are not recommended

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

In strict-mode, is it possible to assign or use a variable before defining it?

A

No, it’s not possible

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

What is the difference between “var”, “let” and “const”?

A

let – is a modern variable declaration. It’s block-scoped.
var – is an old-school variable declaration. Normally we don’t use it at all. It’s function scoped.
const – is like let, but the value of the variable can’t be changed. It’s also block-scoped.

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

A variable in JavaScript can contain any data. True of false?

A

True

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

What are the basic data types in JS?

A

Number, String, Boolean, null value, undefined value, Object, Symbol

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

How do you represent a float in JS?

A

With a Number variable

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

Besides regular numbers, what are special values for a numeric variable?

A

Infinity, -Infinity and NaN

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

What is the result of the following code?

alert( 1 / 0 );

A

Infinity

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

What is the result of the following code?

alert( “not a number” / 2 + 5 );

A

NaN

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

What is the result of the following code?

alert( the result is ${1 + 2} );

A

“the result is 3”

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

What is the result of the following code?

alert( “the result is ${1 + 2}” );

A

“the result is ${1 + 2}”

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

What is the difference between “null” and “undefined”?

A

Normally, we use null to assign an “empty” or “unknown” value to a variable, and we use undefined for checks like seeing if a variable has been assigned.

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

Is “typeof x” the same as “typeof(x)”?

A

Yes, it’s the same

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

What returns “typeof”?

A

A string with the name of the type of the evaluated variable

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

What does this return?

typeof undefined

A

“undefined”

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

What does this return?

typeof 0

A

“number”

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

What does this return?

typeof Symbol(“id”)

A

“symbol”

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

What does this return?

typeof Math

A

“object”

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

What does this return?

typeof null

A

“object”

(That’s wrong. It is an officially recognized error in typeof, kept for compatibility. Of course, null is not an object. It is a special value with a separate type of its own. So, again, this is an error in the language.)

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

What does this return?

typeof alert

A

“function”

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

What does this return?

alert( “6” / “2” );

A

3

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

How do you explicitly convert a value into a number?

A

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

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

How do you explicitly convert a value into a string?

A

let num = String(str); // becomes a number

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

What is the value of age?

let age = Number(“an arbitrary string instead of a number”);

A

NaN

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

What does this return?

alert( 1 + ‘2’ );

A

“12”

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

What does this return?

Boolean(0)

A

false

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

What does this return?

Boolean(‘’)

A

false

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

What does this return?

Boolean(null)

A

false

40
Q

What does this return?

Boolean(undefined)

A

false

41
Q

What does this return?

Boolean(NaN)

A

false

42
Q

What does this return?

Boolean(“0”)

A

true

43
Q

What does this return?

Boolean(“ “)

A

true

44
Q

What happens if you concatenate a number and a string (or a string plus a number)?

A

It returns a string. it doesn’t matter whether the first operand is a string or the second one. The rule is simple: if either operand is a string, the other one is converted into a string as well.

45
Q

What does this return?

alert(2 + 2 + ‘1’ );

A

“41”

46
Q

What does this return?

alert( 2 - ‘1’ );

A

1

47
Q

What does this return?

alert( ‘6’ / ‘2’ );

A

3

48
Q

What does this return?

alert( +true );

A

1

49
Q

What does this return?

alert( +”” );

A

0

50
Q

What does the unary (+) plus operator?

A

The unary plus or, in other words, the plus operator + applied to a single value, doesn’t do anything to numbers. But if the operand is not a number, the unary plus converts it into a number.

51
Q

What does this return?

let apples = "2";
let oranges = "3";

alert( +apples + +oranges );

A

5

52
Q

In the expression “+apples + +oranges” what is executed first? The addition or the unary operators?

A

The unary operators

53
Q

What does this return?

a = b = c = 2 + 2;

A

a=4, b=4. c=4

54
Q
What does this return?
let x = 0;
if (x=1) {
  return x;
}
A

1

55
Q

What does this return?

alert( 8 % 3 );

A

2

56
Q

What does this return?

alert( 2 ** 3 );

A

8 (2 * 2 * 2)

57
Q

What is the value of “a”?

let counter = 1;
let a = ++counter;
A

2

58
Q

What is the value of “a”?

let counter = 1;
let a = counter++;
A

1

59
Q

What does this return?

let counter = 1;
alert( 2 * ++counter );
A

4

60
Q

What does this return?

let counter = 1;
alert( 2 * counter++ );
A

2

61
Q

What’s the value of n?

let n = 2;

A

7

62
Q

What’s the value of n?

let n = 2;
n *= 2;
A

14

63
Q

What will be printed?

let a = (1 + 2, 3 + 4);

alert( a );

A

7 (only the last element after the comma and the parenthesis have higher precedence)

64
Q

What will be printed?

let a = 1 + 2, 3 + 4

alert( a );

A

3, it’s like t’s like (a = 1 + 2), 3 + 4.

65
Q

How does JavaScript compare to strings in a n expression like this:

‘Glow’ > ‘Glee’

A

JavaScript uses the so-called “dictionary” or “lexicographical” order (Unicode).

In other words, strings are compared letter-by-letter.

(It resolves to true)

66
Q

What does the following comparison returns?

alert( ‘2’ > 1 );

A

true, string ‘2’ becomes a number 2

67
Q

What does the following comparison returns?

alert( ‘01’ == 1 );

A

true, string ‘01’ becomes a number 1

68
Q

What does the following comparison returns?

alert( true == 1 );

A

true

69
Q

What does the following comparison returns?

alert( false == 0 );

A

true

70
Q

What happens when you compares values of different types

A

When comparing values of different types, JavaScript converts the values to numbers.

71
Q

What is the difference between == and === ?

A

Different types are converted to numbers by the equality operator ==. An empty string, just like false, becomes a zero.

A strict equality operator === checks the equality without type conversion.

72
Q

Whats does this return?

null === undefined

A

false, both have different types

73
Q

Whats does this return?

null == undefined

A

true

74
Q

What is the unique behavior of comparing null and undefined?

A

There’s a special rule. These two are a “sweet couple”: they equal each other (in the sense of ==), but not any other value.

75
Q

Explain the following results:

alert( null > 0 ); // (1) false
alert( null == 0 ); // (2) false
alert( null >= 0 ); // (3) true

A

Comparisons convert null to a number, treating it as 0. That’s why (3) null >= 0 is true and (1) null > 0 is false.

For == (without any convertions), null is only equals to undefined

76
Q

What is the result of comparing NaN to anything else?

A

Comparing NaN to anything else will always return false

77
Q

Explain the following results:

alert( undefined > 0 ); // false (1)
alert( undefined < 0 ); // false (2)
alert( undefined == 0 ); // false (3)

A

Comparisons (1) and (2) return false because undefined gets converted to NaN and NaN is a special numeric value which returns false for all comparisons.

The equality check (3) returns false because undefined only equals null, undefined, and no other value.

78
Q

What type of variable does the comparison operators return?

A

boolean

79
Q

What is the difference between the functions “alert”, “prompt” and “confirm”?

A

“alert” shows a message.

“prompt” shows a message asking the user to input text. It returns the text or, if Cancel button or Esc is clicked, null.

“confirm” shows a message and waits for the user to press “OK” or “Cancel”. It returns true for OK and false for Cancel/Esc.

80
Q

What happens with values inside parenthesis in “if” operators?

if (x)

A

they are converted to boolean

81
Q

What does the following code do?

(company == ‘Netscape’) ?
alert(‘Right!’) : alert(‘Wrong.’);

A

It’s a short-hand version of if: If company is equal to ‘Netscape’, the alert will display ‘Right!’, else it will display ‘Wrong.’

82
Q

What is the value of “result”?

result = a || b;

A

if a is true, a, else b

83
Q

What is the value of “result”, if ‘a’, ‘b’, ‘c’, ‘d’ are a;; false?

result = a || b || c || d;

A

The last value is returned, which is false

84
Q

What’s the value of “x”?

let x;
true || (x = 1);
alert(x);

A

undefined, because (x = 1) not evaluated due the short circuit

85
Q

How does the AND (&&) operator works?

A

AND returns the first falsy value or the last value if none were found.

When all values are truthy, the last value is returned:

86
Q

What’s the difference between “break” and “continue”?

A

break exits the loop, while continue skips to the next iteration

87
Q

What does the following do?

(i > 5) ? alert(i) : continue;

A

Syntax error.

Syntax constructs that are not expressions cannot be used with the ternary operator ?. In particular, directives such as break/continue aren’t allowed there.

88
Q

What does the following code do on break?

outer: for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    let input = prompt(`Value at coords (${i},${j})`, '');
    if (!input) break outer; // (*)
    // do something with the value...
  }
}
alert('Done!');
A

if an empty string or canceled, then break out of both loops

89
Q

What’s the final value of “userName”?

let userName = ‘John’;

function showMessage() {
  let userName = "Bob"; // declare a local variable
  let message = 'Hello, ' + userName; // Bob
  alert(message);
}

// the function will create and use its own userName
showMessage();
alert( userName );

A

“John” as the “userName” variable internal to the function shadowed the external one

90
Q

What will the alert print?

function showMessage(from, text = “no text given”) {
alert( from + “: “ + text );
}
showMessage(“Ann”);

A

“Ann: no text given”

91
Q

What does a function without a “return” statement return?

A

A function with an empty return or without it returns undefined

92
Q

What does the alert print?

function sayHi() {
  alert( "Hello" );
}

alert( sayHi );

A

The function code

93
Q

A Function Expression is created when the execution reaches it and is usable only from that moment.

True or false?

A

True.

Once the execution flow passes to the right side of the assignment let sum = function… – here we go, the function is created and can be used (assigned, called, etc. ) from now on.

Function Declarations are different.

A Function Declaration can be called earlier than it is defined.

94
Q

Can a function declared inside an “if” block be accessed outside it?

A

In strict mode, when a Function Declaration is within a code block, it’s visible everywhere inside that block. But not outside of it.

95
Q

Is this a valid arrow function?

let double = n => n * 2;

A

Yes, it is valid