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