JavaScript Essentials Flashcards
Learn specifics of the vanilla implementation of JavaScript
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.
document.addEventListener(“DOMContentLoaded”, function() { … });
Event executed when not only HTML is loaded, but also all the external resources: images, styles etc.
window.onload = function() { … }
JavaScript programs can be inserted into any part of an HTML document. True or false?
True
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?
True
The type and language attributes are required in a tag. True or false?
False
Will this code run correctly?:
alert(“There will be an error”)
[1, 2].forEach(alert)
No.
The error occurs because JavaScript does not assume a semicolon before square brackets […].
Nested comments are supported (there may be /…/ inside another /…/). True or False
False
The directive to set strict-mode looks like a string: “use strict” or ‘use strict’. True or False
True
The “use strict” directive switches the engine to the “modern” mode, changing the behavior of some built-in features. True or False
True
What are the naming rules for variables in JS?
The name must contain only letters, digits, or the symbols $ and _.
The first character must not be a digit.
Variables are case sensitive. True or false?
True
Are non-Latin letters are allowed in variables?
Yes, but they are not recommended
In strict-mode, is it possible to assign or use a variable before defining it?
No, it’s not possible
What is the difference between “var”, “let” and “const”?
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.
A variable in JavaScript can contain any data. True of false?
True
What are the basic data types in JS?
Number, String, Boolean, null value, undefined value, Object, Symbol
How do you represent a float in JS?
With a Number variable
Besides regular numbers, what are special values for a numeric variable?
Infinity, -Infinity and NaN
What is the result of the following code?
alert( 1 / 0 );
Infinity
What is the result of the following code?
alert( “not a number” / 2 + 5 );
NaN
What is the result of the following code?
alert( the result is ${1 + 2}
);
“the result is 3”
What is the result of the following code?
alert( “the result is ${1 + 2}” );
“the result is ${1 + 2}”
What is the difference between “null” and “undefined”?
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.
Is “typeof x” the same as “typeof(x)”?
Yes, it’s the same
What returns “typeof”?
A string with the name of the type of the evaluated variable
What does this return?
typeof undefined
“undefined”
What does this return?
typeof 0
“number”
What does this return?
typeof Symbol(“id”)
“symbol”
What does this return?
typeof Math
“object”
What does this return?
typeof null
“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.)
What does this return?
typeof alert
“function”
What does this return?
alert( “6” / “2” );
3
How do you explicitly convert a value into a number?
let num = Number(str); // becomes a number
How do you explicitly convert a value into a string?
let num = String(str); // becomes a number
What is the value of age?
let age = Number(“an arbitrary string instead of a number”);
NaN
What does this return?
alert( 1 + ‘2’ );
“12”
What does this return?
Boolean(0)
false
What does this return?
Boolean(‘’)
false
What does this return?
Boolean(null)
false
What does this return?
Boolean(undefined)
false
What does this return?
Boolean(NaN)
false
What does this return?
Boolean(“0”)
true
What does this return?
Boolean(“ “)
true
What happens if you concatenate a number and a string (or a string plus a number)?
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.
What does this return?
alert(2 + 2 + ‘1’ );
“41”
What does this return?
alert( 2 - ‘1’ );
1
What does this return?
alert( ‘6’ / ‘2’ );
3
What does this return?
alert( +true );
1
What does this return?
alert( +”” );
0
What does the unary (+) plus operator?
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.
What does this return?
let apples = "2"; let oranges = "3";
alert( +apples + +oranges );
5
In the expression “+apples + +oranges” what is executed first? The addition or the unary operators?
The unary operators
What does this return?
a = b = c = 2 + 2;
a=4, b=4. c=4
What does this return? let x = 0; if (x=1) { return x; }
1
What does this return?
alert( 8 % 3 );
2
What does this return?
alert( 2 ** 3 );
8 (2 * 2 * 2)
What is the value of “a”?
let counter = 1; let a = ++counter;
2
What is the value of “a”?
let counter = 1; let a = counter++;
1
What does this return?
let counter = 1; alert( 2 * ++counter );
4
What does this return?
let counter = 1; alert( 2 * counter++ );
2
What’s the value of n?
let n = 2;
7
What’s the value of n?
let n = 2; n *= 2;
14
What will be printed?
let a = (1 + 2, 3 + 4);
alert( a );
7 (only the last element after the comma and the parenthesis have higher precedence)
What will be printed?
let a = 1 + 2, 3 + 4
alert( a );
3, it’s like t’s like (a = 1 + 2), 3 + 4.
How does JavaScript compare to strings in a n expression like this:
‘Glow’ > ‘Glee’
JavaScript uses the so-called “dictionary” or “lexicographical” order (Unicode).
In other words, strings are compared letter-by-letter.
(It resolves to true)
What does the following comparison returns?
alert( ‘2’ > 1 );
true, string ‘2’ becomes a number 2
What does the following comparison returns?
alert( ‘01’ == 1 );
true, string ‘01’ becomes a number 1
What does the following comparison returns?
alert( true == 1 );
true
What does the following comparison returns?
alert( false == 0 );
true
What happens when you compares values of different types
When comparing values of different types, JavaScript converts the values to numbers.
What is the difference between == and === ?
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.
Whats does this return?
null === undefined
false, both have different types
Whats does this return?
null == undefined
true
What is the unique behavior of comparing null and undefined?
There’s a special rule. These two are a “sweet couple”: they equal each other (in the sense of ==), but not any other value.
Explain the following results:
alert( null > 0 ); // (1) false
alert( null == 0 ); // (2) false
alert( null >= 0 ); // (3) true
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
What is the result of comparing NaN to anything else?
Comparing NaN to anything else will always return false
Explain the following results:
alert( undefined > 0 ); // false (1)
alert( undefined < 0 ); // false (2)
alert( undefined == 0 ); // false (3)
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.
What type of variable does the comparison operators return?
boolean
What is the difference between the functions “alert”, “prompt” and “confirm”?
“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.
What happens with values inside parenthesis in “if” operators?
if (x)
they are converted to boolean
What does the following code do?
(company == ‘Netscape’) ?
alert(‘Right!’) : alert(‘Wrong.’);
It’s a short-hand version of if: If company is equal to ‘Netscape’, the alert will display ‘Right!’, else it will display ‘Wrong.’
What is the value of “result”?
result = a || b;
if a is true, a, else b
What is the value of “result”, if ‘a’, ‘b’, ‘c’, ‘d’ are a;; false?
result = a || b || c || d;
The last value is returned, which is false
What’s the value of “x”?
let x;
true || (x = 1);
alert(x);
undefined, because (x = 1) not evaluated due the short circuit
How does the AND (&&) operator works?
AND returns the first falsy value or the last value if none were found.
When all values are truthy, the last value is returned:
What’s the difference between “break” and “continue”?
break exits the loop, while continue skips to the next iteration
What does the following do?
(i > 5) ? alert(i) : continue;
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.
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!');
if an empty string or canceled, then break out of both loops
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 );
“John” as the “userName” variable internal to the function shadowed the external one
What will the alert print?
function showMessage(from, text = “no text given”) {
alert( from + “: “ + text );
}
showMessage(“Ann”);
“Ann: no text given”
What does a function without a “return” statement return?
A function with an empty return or without it returns undefined
What does the alert print?
function sayHi() { alert( "Hello" ); }
alert( sayHi );
The function code
A Function Expression is created when the execution reaches it and is usable only from that moment.
True or false?
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.
Can a function declared inside an “if” block be accessed outside it?
In strict mode, when a Function Declaration is within a code block, it’s visible everywhere inside that block. But not outside of it.
Is this a valid arrow function?
let double = n => n * 2;
Yes, it is valid