STATEMENTS Flashcards

1
Q

1) What is the difference btw expressions and statements?:-

A

Expressions are evaluated to produce a value, but statements are executed to make something happen.

  • One way to “make something happen” is to evaluate an expression that has side effects. Expressions with side effects, such as assignments and function invocations, can stand alone as statements, and when used this way are known as expression statements. A similar category of statements are the declaration statements that declare new variables and define new functions.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

2) JavaScript programs are nothing more than a sequence of statements to execute. By default, the JavaScript
interpreter executes these statements one after another in the order they are written. Another way to “make
something happen” is to alter this default order of execution, and JavaScript has a number of statements or
control structures that do just this. Name the three groups and examples?:-

A

-> Conditionals statements like if and switch that make the JavaScript interpreter execute or skip other
statements depending on the value of an expression
-> Loops statements like while and for that execute other statements repetitively.
-> Jumps statements like break, return, and throw that cause the interpreter to jump to another part of the
program

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

3) The empty statement allows you to?:-

A

include no statements where one is expected. The empty statement looks like this:

;

  • The JavaScript interpreter takes no action when it executes an empty statement. The empty statement is
    occasionally useful when you want to create a loop that has an empty body. Consider the following for loop:

// Initialize an array a
for(let i = 0; i < a.length; a[i++] = 0) /* empty */ ;

  • In this loop, all the work is done by the expression a[i++] = 0, and no loop body is necessary. JavaScript
    syntax requires a statement as a loop body, however, so an empty statement—just a bare semicolon—is used.
    When you intentionally use the empty statement, it is a good idea to comment your code in a way that makes it
    clear that you are doing it on purpose.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

4) The rule in JavaScript (as in most programming languages) is that by default an else clause is part of the ___ if statement?:-

A

nearest (even though you can remove the {}, it is alway best to use then, because it become clear which else goes with which if).

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

5) The if/else statement evaluates an expression and executes one of two pieces of code, depending on the outcome. But what about when you need to execute one of many pieces of code? One way to do this is with an?:-

A

else if statement. else if is not really a JavaScript statement, but simply a frequently used programming idiom
that results when repeated if/else statements are used:

if (n === 1) {
// Execute code block #1
} else if (n === 2) {
// Execute code block #2
} else if (n === 3) {
// Execute code block #3
} else {
// If all else fails, execute block #4
}

the alternative is the fully nested form:
if (n === 1) {
// Execute code block #1
}
else {
if (n === 2) {
// Execute code block #2
}
else {
if (n === 3) {
// Execute code block #3
}
else {
// If all else fails, execute block #4
}
}
}

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

6) An if statement causes a branch in the flow of a program’s execution, and you can use the else if idiom to
perform a multiway branch. This is not the best solution, however, when all of the branches depend on the value of the same expression. In this case, it is wasteful to repeatedly evaluate that expression in multiple
if statements. which statement handles this situation best?:-

A

The switch statement handles exactly this situation. The switch keyword is followed by an expression in
parentheses and a block of code in curly braces:

switch(expression) {
statements
}

  • Sameness is determined by the === operator, so the expressions must match without any type conversion.
  • The case clauses in a switch statement specify only the starting point of the desired code;
  • When using switch inside a function, however, you may use a return statement instead of a break statement. Both serve to terminate the switch statement and prevent execution from falling through to the next case.

function convert(x) {
switch (typeof x) {
case “number”: // Convert the number to a hexadecimal integer
return x.toString(16);
case “string”: // Return the string enclosed in quotes
return ‘”’ + x + ‘”’;
default: // Convert any other type in the usual way
return String(x);
}
}

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

7) Because not all of the case expressions are evaluated each time the switch statement is executed, you should avoid using case expressions that contain?:-

A

side effects such as function calls or assignments. The safest course is simply to limit your case expressions
to constant expressions.

  • Note that, the default: label appears at the end of the switch body, following all the case labels. This is a
    logical and common place for it, but it can actually appear anywhere within the body of the statement.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

8) JavaScript has five looping statements name then?:-

A

while, do/while, for, for/of (and its for/await variant), and for/in.

  • The do while loop must always be terminated with a semicolon. The while loop doesn’t need a semicolon if the loop body is enclosed in curly braces.
  • for(;;) is another way of writing an infinite loop, like while(true).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

9) ES6 defines a new loop statement: for/of. This new kind of loop uses the for key‐word but is a completely
different kind of loop than the regular for loop. It is also completely different than the older for/in loop.
How does it work?:-

A

The for/of loop works with iterable objects. We’ll explain later exactly what it means for an object to be
iterable, but for now, it is enough to know that arrays, strings, sets, and maps are iterable: they represent a
sequence or set of elements that you can loop or iterate through using a for/of loop.

Here, for example, is how we can use for/of to loop through the elements of an array of numbers and compute
their sum:

let data = [1, 2, 3, 4, 5, 6, 7, 8, 9], sum = 0;
for(let element of data) {
sum += element;
}
sum // => 45

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

10) (for/of with objects) Objects are not (by default) iterable. What happens when you attempt to use for/of on a regular object?:-

A

a TypeError is thrown at runtime:

let o = { x: 1, y: 2, z: 3 };
for(let element of o) { // Throws TypeError because o is not iterable
console.log(element);
}

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

11) If you want to iterate through the properties of an object, you can use(name the two loops and condtions
that have to be meet for each loop, if any)?:-

A

-> the for/in loop,
-> or use for/of with the Object.keys() method(Because by default ordinary objects are not iterable):

let o = { x: 1, y: 2, z: 3 };
for(let element of o) { // Throws TypeError because o is not iterable
console.log(element);
}

let o = { x: 1, y: 2, z: 3 }; let keys = “”;
for(let k of Object.keys(o)) {
keys += k; }
keys // => “xyz”

  • This works because Object.keys() returns an array of property names for an object, and arrays are iterable
    with for/of. Note also that this iteration of the keys of an object is not live as an array would be, changes
    to the object o made in the loop body will have no effect on the iteration.
  • If you don’t care about the keys of an object, you can also iterate through their corresponding values like
    this:

let sum = 0;
for(let v of Object.values(o)) {
sum += v;
}
sum // => 6

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

12) If you are interested in both the keys and the values of an object’s properties, you can use for/of with
Object.entries() and destructuring assignment. Show code implimentation?:-

A

let o = { x: 1, y: 2, z: 3 };
let pairs = “”;
for(let [k, v] of Object.entries(o)) {
pairs += k + v;
}
pairs // => “x1y2z3”

  • Object.entries() returns an array of arrays, where each inner array represents a key/value pair for one
    property of the object. We use destructuring assignment in this code example to unpack those inner arrays into two individual variables.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

13) (for/of with strings) Strings are iterable character-by-character in ES6. Implement a letter frequence
counter program for the word “mississippi” ?:-

A

let frequency = {};
for(let letter of “mississippi”) {
if (frequency[letter]) {
frequency[letter]++;
} else {
frequency[letter] = 1;
}
}
frequency // => {m: 1, i: 4, s: 4, p: 2}

  • Note that strings are iterated by Unicode codepoint, not by UTF-16 character. The string “I ❤ do emoji ”
    has a .length of 5 (because the two emoji characters each require two UTF-16 characters to represent). But if
    you iterate that string with for/of, the loop body will run three times, once for each of the three code points
    “I”, “❤”, and “ dog agian .”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

14) The built-in ES6 Set and Map classes are iterable. When you iterate a Set with for/of, the loop body runs
once for each element of the set. You could use code like this to print the unique words in a string of text?:-

A

let text = “Na na na na na na na na Batman!”;
let wordSet = new Set(text.split(“ “));
let unique = [];
for (let word of wordSet) {
unique.push(word);
}
unique // => [“Na”, “na”, “Batman!”]

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

15) Maps are an interesting case because the iterator for a Map object does not iterate the Map keys, or the
Map values, but __________. Each time through the iteration, the iterator returns?:-

A

-> key/value pairs an array whose first element is a key and whose second element is the corresponding value. Given a Map m, you could iterate and destructure its key/value pairs like this:

let m = new Map([[1, “one”]]);
for(let [key, value] of m) {
key // => 1
value // => “one”
}

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

16) ES2018 introduces a new kind of iterator, known as an asynchronous iterator, a variant of the?:-

A

for/of loop, known as the for/await loop that works with asynchronous iterators.

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

17) A for/in loop looks a lot like a for/of loop, with the of keyword changed to in. While a for/of loop
requires an iterable object after the of, a for/in loop works with?:-

A

any object after the in. The for/in statement loops through the property names of a specified object. The syntax looks like this:

for (variable in object) {
statement
}

let o = { x: 1, y: 2, z: 3 };
let a = [], i = 0;
for(a[i++] in o) /* empty */;

  • The for/of loop is new in ES6, but for/in has been part of JavaScript since the very beginning (which is why
    it has the more natural sounding syntax).

And you might use a for/in loop like this:
for(let p in o) { // Assign property names of o to variable p
console.log(o[p]); // Print the value of each property
}

  • I find that a common source of bugs in my own code is the accidental use of for/in with arrays when I meant to use for/of. When working with arrays, you almost always want to use for/of instead of for/in.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

18) The for/in loop does not actually enumerate all properties of an object. It does not enumerate properties
whose names are?:-

A

-> Symbols.
-> And of the properties whose names are strings, it only loops over the enumerable properties.

  • The various built-in methods defined by core JavaScript are not enumerable. All objects have a toString()
    method, for example, but the for/in loop does not enumerate this toString property. In addition to built-in
    methods, many other properties of the built-in objects are non-enumerable. All properties and methods defined by your code are enumerable, by default. (You can make them non-enumerable using techniques tested later).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

19) Enumerable inherited properties are also enumerated by the for/in loop. This means that if you use for/in
loops and also use code that defines properties that are inherited by all objects, then your loop may not behave in the way you expect. For this reason, many programmers prefer?:-

A

to use a for/of loop with Object.keys() instead of a for/in loop.

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

20) If the body of a for/in loop deletes a property that has not yet been enumerated, that property __________.
If the body of the loop defines new properties on the object, those properties?:-

A

-> will not be enumerated.
-> may or may not be enumerated.

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

21) Another category of JavaScript statements are jump statements. As the name implies, these cause the
JavaScript interpreter to?:-

A

jump to a new location in the source code.

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

23) Any statement may be labeled by preceding it with an identifier and a colon. By labeling a statement, you
give it a name that you can use to refer to it elsewhere in your program. You can label any statement, although it is only useful to label statements that?:-

A

have bodies, such as loops and conditionals.

  • By giving a loop a name, you can use break and continue statements inside the body of the loop to exit the
    loop or to jump directly to the top of the loop to begin the next iteration. break and continue are the only
    JavaScript statements that use statement label.

identifier: statement

mainloop: while(token !== null) {
// Code omitted…
continue mainloop; // Jump to the next iteration of the named loop
// More code omitted…
}

  • A statement may not have the same label as a statement that contains it, but two statements may have the same label as long as neither one is nested within the other. Labeled statements may themselves be labeled.
    Effectively, this means that any statement may have multiple labels.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

22 == 23 change

A

Change

24
Q

24) The break statement, used alone, causes?:-

A

the innermost enclosing loop or switch statement to exit immediately. Its syntax is simple:

break;

  • Because it causes a loop or switch to exit, this form of the break statement is legal only if it appears
    inside one of these statements.

for(let i = 0; i < a.length; i++) {
if (a[i] === target) break;
}

The loop terminates in the normal way when it reaches the end of the array; it terminates with a break statement if it finds what it is looking for in the array:

25
Q

25) JavaScript also allows the break keyword to be followed by a statement label(just the identifier, with no
colon): break labelname; When break is used with a label, it?:-

A

jumps to the end of, or terminates, the enclosing statement that has the specified label. It is a syntax error
to use break in this form if there is no enclosing statement with the specified label.

  • With this form of the break statement, the named statement need not be a loop or switch: break can “break out of” any enclosing statement. This statement can even be a statement block grouped within curly braces for the sole purpose of naming the block with a label.
  • You need the labeled form of the break statement when you want to break out of a statement that is not the nearest enclosing loop or a switch. The following code demonstrates:

let matrix = getData(); // Get a 2D array of numbers from somewhere
// Now sum all the numbers in the matrix.
let sum = 0, success = false;
// Start with a labeled statement that we can break out of if errors occur
computeSum: if (matrix) {
for (let x = 0; x < matrix.length; x++) {
let row = matrix[x];
if (!row) break computeSum;
for (let y = 0; y < row.length; y++) {
let cell = row[y];
if (isNaN(cell)) break computeSum; sum += cell;
}
}
success = true;
}
// The break statements jump here. If we arrive here with success == false
// then there was something wrong with the matrix we were given.
// Otherwise, sum contains the sum of all cells of the matrix.

  • Finally, note that a break statement, with or without a label, can not transfer control across function
    boundaries. You cannot label a function definition statement, for example, and then use that label inside the
    function.
26
Q

26) The continue statement is similar to the break statement. Instead of exiting a loop, however, continue?:-

A

restarts a loop at the next iteration.

The continue statement’s syntax is just as simple as the break statement’s:

continue;

27
Q

27) The continue statement can also be used with a label: continue labelname; The continue statement, in both its labeled and unlabeled forms, can be used only?:-

A

within the body of a loop. Using it anywhere else causes a syntax error.

28
Q

28) When the continue statement is executed, the current iteration of the enclosing loop is terminated, and the next iteration begins. This means different things for different types of loops. Explain?:-

A

-> In a while loop, the specified expression at the beginning of the loop is tested again, and if it’s true,
the loop body is executed starting from the top.

-> In a do/while loop, execution skips to the bottom of the loop, where the loop condition is tested again
before restarting the loop at the top.

-> In a for loop, the increment expression is evaluated, and the test expression is tes‐ ted again to determine
if another iteration should be done.

-> In a for/of or for/in loop, the loop starts over with the next iterated value or next property name being
assigned to the specified variable.

  • Note the difference in behavior of the continue statement in the while and for loops: a while loop returns
    directly to its condition, but a for loop first evaluates its incre‐ ment expression and then returns to its
    condition. Because the continue state‐ ment behaves differently for these two loops, however, it is not actually possible to perfectly simulate a for loop with a while loop alone.

for(let i = 0; i < data.length; i++) {
if (!data[i]) continue; // Can’t proceed with undefined data
total += data[i];
}

  • Like the break statement, the continue statement can be used in its labeled form within nested loops when the loop to be restarted is not the immediately enclosing loop. Also, as with the break statement, line breaks are not allowed between the con tinue statement and its labelname.
29
Q

29) Recall that function invocations are expressions and that all expressions have values. A return statement
within a function specifies the value of invocations of that func‐ tion. What is the syntax of the return
statement?:-

A

return expression;

function square(x) { return x*x; } // A function that has a return statement square(2) // => 4

30
Q

30) A return statement may appear only within?:-

A

the body of a function.

  • It is a syntax error for it to appear anywhere else.
  • Because of JavaScript’s automatic semicolon insertion, you cannot include a line break between the return
    keyword and the expression that follows it.
31
Q

31) The yield statement is much like the return statement but?:-

A

is used only in ES6 generator functions to produce the next value in the generated sequence of values without actually returning:

// A generator function that yields a range of integers
function* range(from, to) {
for (let i = from; i <= to; i++) {
yield i;
}
}

32
Q

31==31

A

Change

33
Q

33) An exception is a signal that?:-

A

indicates that some sort of exceptional condition or error has occurred.

  • To throw an exception is to signal such an error or exceptional condition.
34
Q

34) To catch an exception is to?:-

A

handle it—to take whatever actions are necessary or appropriate to recover from the exception.

35
Q

35) In JavaScript, exceptions are thrown whenever(name 2 conditions)?:-

A

-> a runtime error occurs,
-> and whenever the program explicitly throws one using the throw statement.

  • The throw statement has the following syntax:
  • expression may evaluate to a value of any type. You might throw a number that represents an error code or a string that contains a human-readable error message.

throw expression;

  • The Error class and its subclasses are used when the JavaScript interpreter itself throws an error, and you
    can use them as well. An Error object has a name property that specifies the type of error and a message property that holds the string passed to the constructor function. Here is an example function that throws an Error object when invoked with an invalid argument:

function factorial(x) {
// If the input argument is invalid, throw an exception!
if (x < 0) throw new Error(“x must not be negative”); // Otherwise, compute a value and return normally
let f;
for (f = 1; x > 1; f = x, x–) / empty */;
return f;
}
factorial(4) // => 24

36
Q

36) Exceptions are caught with the?:-

A

try/catch/finally statement.

37
Q

37) When an exception is thrown, the JavaScript interpreter immediately?:-

A

stops normal program execution and jumps to the nearest exception handler.

  • Exception handlers are written using the catch clause of the try/catch/finally statement.
38
Q

38) If the block of code in which an exception was thrown does not have an associated catch clause, the
interpreter checks the next-highest enclosing block of code to see if it has an exception handler associated
with it. This continues until a handler is found. If an exception is thrown in a function that does not contain
a try/catch/finally statement to handle it, the exception?:-

A

propagates up to the code that invoked the function. In this way, exceptions propagate up through the lexical
structure of JavaScript methods and up the call stack. If no exception handler is ever found, the exception is
treated as an error and is reported to the user.

39
Q

39) The try/catch/finally statement is JavaScript’s?:-

A

exception handling mechanism.

  • The try, catch, and finally blocks all begin and end with curly braces. These braces are a required part of
    the syntax and cannot be omitted, even if a clause contains only a single statement.
  • The following code illustrates the syntax and purpose of the try/catch/finally statement:

try {
// Normally, this code runs from the top of the block to the bottom // without problems. But it can sometimes throw an exception,
// either directly, with a throw statement, or indirectly, by calling // a method that throws an exception.
}
catch (e) {
// The statements in this block are executed if, and only if, the try
// block throws an exception. These statements can use the local variable
// e to refer to the Error object or other value that was thrown.
// This block may handle the exception somehow, may ignore the
// exception by doing nothing, or may rethrow the exception with throw.

}
finally {
// This block contains statements that are always executed, regardless of
// what happens in the try block. They are executed whether the try
// block terminates:
// 1. normally, after reaching the bottom of the block
// 2. because of a break, continue, or return statement
// 3. with an exception that is handled by a catch clause above
// 4. with an uncaught exception that is still propagating
}

40
Q

40) The try clause simply defines the?:-

A

block of code whose exceptions are to be handled.

  • Both the catch and finally blocks are optional, but a try block must be accompanied by at least one of these
    blocks.
41
Q

41) The try block is followed by a catch clause, which is a?:-

A

block of statements that are invoked when an exception occurs anywhere within the try block.

  • Note that the catch keyword is generally followed by an identifier in parentheses. This identifier is like a
    function parameter. When an exception is caught, the value associated with the exception (an Error object, for
    example) is assigned to this parameter. The identifier associated with a catch clause has block scope—it is only defined within the catch block.
42
Q

42) The catch clause is followed by a finally block containing?:-

A

cleanup code that is guaranteed to be executed, regardless of what happens in the try block.

  • In the normal case, the JavaScript interpreter reaches the end of the try block and then proceeds to the
    finally block, which performs any necessary cleanup. If the interpreter left the try block because of a return,
    continue, or break statement, the finally block is executed before the interpreter jumps to its new destination.
  • If an exception occurs in the try block and there is an associated catch block to handle the exception, the
    interpreter first executes the catch block and then the finally block. If there is no local catch block to
    handle the exception, the interpreter first executes the finally block and then jumps to the nearest containing
    catch clause.
  • If a finally block itself causes a jump with a return, continue, break, or throw statement, or by calling a
    method that throws an exception, the interpreter abandons whatever jump was pending and performs the new jump. For example, if a finally clause throws an exception, that exception replaces any exception that was in the process of being thrown. If a finally clause issues a return statement, the method returns normally, even if an exception has been thrown and has not yet been handled.
43
Q

43) try and finally can be used together without a catch clause. In this case, the finally block is simply?:-

A

the cleanup code that is guaranteed to be executed, regardless of what happens in the try block.

44
Q

44) Recall that we can’t completely simulate a for loop with a while loop because the continue statement behaves differently for the two loops. If we add a?:-

A

try/finally statement, we can write a while loop that works like a for loop and that handles continue statements correctly:

// Simulate for(initialize ; test ;increment ) body; initialize ;
while (test) {
try { body; }
finally { increment; }
}

  • Note, however, that a body that contains a break statement behaves slightly differently (causing an extra
    increment before exiting) in the while loop than it does in the for loop, so even with the finally clause, it is
    not possible to completely simulate the for loop with while.
45
Q

45) Occasionally you may find yourself using a catch clause solely to detect and stop the propagation of an
exception, even though you do not care about the type or the value of the exception. In ES2019 and later, you
can omit?:-

A

the parentheses and the identifier and use the catch keyword bare in this case. Here is an example:

// Like JSON.parse(), but return undefined instead of throwing an error
function parseJSON(s) {
try {
return JSON.parse(s);
} catch {
// Something went wrong but we don’t care what it was
return undefined;
}
}

46
Q

46) The with statement runs a block of code as if?:-

A

the properties of a specified object were variables in scope for that code. It has the following syntax:

with (object) statement

  • This statement creates a temporary scope with the properties of an object as variables and then executes
    statement within that scope.
47
Q

47) The with statement is forbidden in __________ and should be considered deprecated in non-strict
mode: hence avoid_______. JavaScript code that uses with is?:-

A

-> strict mode
-> using it whenever possible

difficult to optimize and is likely to run significantly more slowly than the equivalent code written without
the with statement.

48
Q

48) The common use of the with statement is to make it easier to work with?:-

A

deeply nested object hierarchies. In client-side JavaScript, for example, you may have to type expressions like
this one to access elements of an HTML form:

document.forms[0].address.value

If you need to write expressions like this a number of times, you can use the with statement to treat the
properties of the form object like variables:

with(document.forms[0]) {
// Access form elements directly here. For example:
name.value = “”;
address.value = “”;
email.value = “”;
}

This reduces the amount of typing you have to do: you no longer need to prefix each form property name with document.forms[0]. It is just as simple, of course, to avoid the with statement and write the preceding code like this:

let f = document.forms[0];
f.name.value = “”;
f.address.value = “”;
f.email.value = “”;

  • Note that if you use const or let or var to declare a variable or constant within the body of a with statement,
    it creates an ordinary variable and does not define a new property within the specified object.
49
Q

49) The debugger statement normally does nothing. If, however, a debugger program is available and is running,then an implementation may (but is not required to)?:-

A

perform some kind of debugging action. In practice, this statement acts like a break‐point: execution of
JavaScript code stops, and you can use the debugger to print variables’ values, examine the call stack, and so
on.

  • Suppose, for example, that you are getting an exception in your function f() because it is being called with
    an undefined argument, and you can’t figure out where this call is coming from. To help you in debugging this
    problem, you might alter f() so that it begins like this:

function f(o) {
if (o === undefined) debugger; // Temporary line for debugging purposes
… // The rest of the function goes here.
}

Now, when f() is called with no argument, execution will stop, and you can use the debugger to inspect the call
stack and find out where this incorrect call is coming from.

  • Note that it is not enough to have a debugger available: the debugger statement won’t start the debugger for you. If you’re using a web browser and have the developer tools console open, however, this statement will cause a breakpoint.
50
Q

50) “use strict” is a directive introduced in ES5. Directives are not statements (but are close enough). There
are two important differences between the “use strict” directive and regular statements?:-

A

-> It does not include any language keywords: the directive is just an expression statement that consists of a
special string literal (in single or double quotes).
-> It can appear only at the start of a script or at the start of a function body, before any real statements
have appeared.

51
Q

51) The purpose of a “use strict” directive is to indicate that the code that follows (in the script or function)?:-

A

is strict code.

  • The top-level (nonfunction) code of a script is strict code if the script has a “use strict” directive.
  • A function body is strict code if it is defined within strict code or if it has a “use strict” directive.
  • Code passed to the eval() method is strict code if eval() is called from strict code or if the string of code
    includes a “use strict” directive.
52
Q

52) In addition to code explicitly declared to be strict, any code in a class body or in an ES6 module is
automatically strict code. This means ?:-

A

that if all of your JavaScript code is written as modules, then it is all automatically strict, and you will
never need to use an explicit “use strict” directive.

53
Q

53) Strict code is executed in strict mode. Strict mode is?:-

A

a restricted subset of the language that fixes important language deficiencies and provides stronger error
checking and increased security. Because strict mode is not the default, old JavaScript code that still uses
the deficient legacy features of the language will continue to run correctly.

54
Q

54) The differences between strict mode and non-strict mode are the following (the first three are particularly important) list 13 differences?:-

A

-> The with statement is not allowed in strict mode.
-> In strict mode, all variables must be declared: a ReferenceError is thrown if you assign a value to an
identifier that is not a declared variable, function, function parameter, catch clause parameter, or property of
the global object. (In non-strict mode, this implicitly declares a global variable by adding a new property to
the global object.)
-> In strict mode, functions invoked as functions (rather than as methods) have a this value of undefined.
(In non-strict mode, functions invoked as functions are always passed the global object as their this value.)
Also, in strict mode, when a function is invoked with call() or apply(), the this value is exactly the value
passed as the first argument to call() or apply(). (In non-strict mode, null and undefined values are replaced
with the global object and nonobject values are converted to objects.)
->In strict mode, assignments to nonwritable properties and attempts to create new properties on non-extensible objects throw a TypeError. (In non-strict mode, these attempts fail silently.)
-> In strict mode, code passed to eval() cannot declare variables or define functions in the caller’s scope as
it can in non-strict mode. Instead, variable and function definitions live in a new scope created for the eval(). This scope is discarded when the eval() returns.
-> In strict mode, the Arguments object in a function holds a static copy of the values passed to the function.
In non-strict mode, the Arguments object has “magical” behavior in which elements of the array and named function parameters both refer to the same value.
-> In strict mode, a SyntaxError is thrown if the delete operator is followed by an unqualified identifier such
as a variable, function, or function parameter. (In nonstrict mode, such a delete expression does nothing and
evaluates to false.)
-> In strict mode, an attempt to delete a nonconfigurable property throws a TypeError. (In non-strict mode, the attempt fails and the delete expression evaluates to false.)
-> In strict mode, it is a syntax error for an object literal to define two or more properties by the same name.
(In non-strict mode, no error occurs.)
-> In strict mode, it is a syntax error for a function declaration to have two or more parameters with the same
name. (In non-strict mode, no error occurs.)
-> In strict mode, octal integer literals (beginning with a 0 that is not followed by an x(i think it should
be an O not X)) are not allowed. (In non-strict mode, some implementations allow octal literals.)
-> In strict mode, the identifiers eval and arguments are treated like keywords, and you are not allowed to
change their value. You cannot assign a value to these identifiers, declare them as variables, use them as
function names, use them as function parameter names, or use them as the identifier of a catch block.
-> In strict mode, the ability to examine the call stack is restricted. arguments.caller and arguments.callee
both throw a TypeError within a strict mode function. Strict mode functions also have caller and arguments
properties that throw TypeError when read. (Some implementations define these nonstandard properties on
non-strict functions.)

55
Q

55)We say that function declarations are “hoisted” because it is as if?:-

A

they had all been moved up to the top of whatever scope they are defined within. The upshot is that code that
invokes a function can exist in your program before the code that declares the function.

  • Unlike functions, class declarations are not hoisted, and you cannot use a class before the declaration.
56
Q

56) The import and export declarations are used together to make?:-

A

values defined in one module of JavaScript code available in another module.

  • A module is a file of Java‐ Script code with its own global namespace, completely independent of all other
    mod‐ules. The only way that a value (such as function or class) defined in one module can be used in another
    module is if the defining module exports it with export and the using module imports it with import.
57
Q

57) Values within a JavaScript module are private and cannot be imported into other modules unless?:-

A

they have been explicitly exported.