JavaScript Conditions - Questions

Pack 4 27 questions

Things to know

  1. JavaScript Conditions unit required

Booleans

  1. How to represent Boolean values in JavaScript?

    Using the true and false keywords. (Keep in mind that these are case-sensitive, so there's nothing such as TRUE and FALSE.) Learn more in JavaScript Conditions — Booleans.

  2. true and false are keywords in JavaScript. True or false?

    True; both true and false are both keywords in JavaScript.

  3. Given that run is to be used as a variable holding a Boolean, is its name OK for this use case?

    Not really. run is a verb, hence it suits as the name of a function more than the name of an identifier holding a Boolean. For this, a better name would be shouldRun, clearly depicting a true/false question.

  4. It's a good practice to create Booleans via new Boolean() in a program given that we know that we have to call certain methods on them. True or false?

    False; new Boolean() must NEVER be used explicitly in code! Learn more in JavaScript Conditions — Booleans.

  5. All relational operators in JavaScript return Booleans, always. True or false?

    True.

  6. All logical operators in JavaScript return Booleans, always. True or false?

    False, || and && return the values of their operands, so if those operands are not Booleans, we don't get Booleans in return. Learn more in JavaScript Operators: Logical operators.

  7. How to convert an arbitrary value into a Boolean in JavaScript?

    Using Boolean() (called as a function) or double negation (!!). Learn more in JavaScript Conditions — Booleans: Converting to Boolean.

  8. Specify the rules of converting numbers to Booleans.

    All numbers in JavaScript except for 0 and NaN convert to the Boolean true; 0 and NaN convert to false. Learn more in JavaScript Conditions — Booleans: Boolean conversion rules.

  9. Specify the rules of converting strings to Booleans.

    All strings except for the empty string ('') convert to the Boolean true; the empty string converts to false. Learn more in JavaScript Conditions — Booleans: Boolean conversion rules.

  10. Specify the rules of converting undefined and null to Booleans.

    Both undefined and null convert to the Boolean false. Learn more in JavaScript Conditions — Booleans: Boolean conversion rules.

  11. Specify the rules of converting objects to Booleans.

    All objects in JavaScript — literally all — convert to the Boolean true. Learn more in JavaScript Conditions — Booleans: Boolean conversion rules.

if...else

  1. The if and else statements can be nested within if and else statements. True or false?

    Absolutely true. In fact, the common else if construct we use routinely in JavaScript is basically just an if nested inside an else statement.

  2. else if is a single keyword in JavaScript. True or false?

    False; else if is an else statement whose body is comprised of an if statement.

  3. In JavaScript, there is a dedicated keyword to denote a statement that evaluates another condition when the preceding if statement fails. True or false? If true, name that keyword.

    There's no such dedicated keyword in JavaScript. We can only use else if which is essentially an if nested inside an else statement.

  4. In the header of an if statement, the expression given inside the pair of parentheses (()) must be converted by us into a Boolean before being used. True or false?

    False; there's no necessity of doing so, for JavaScript automatically coerces the given expression into a Boolean while evaluating the if statement.

switch

  1. What is the purpose of the switch statement?

    The purpose of the switch statement is to execute code conditionally based upon different values of a given expression. Learn more in JavaScript Conditions — switch.

  2. When exactly should we use a switch statement?

    A switch statement must be used when we have an arbitrary value to be tested against a set of different values, reacting differently in each case. Learn more in JavaScript Conditions — switch.

  3. What is the syntax of the switch statement?

    We start off with the switch keyword, followed by a condition wrapped up inside a pair of parentheses (()), followed by a set of curly braces ({}), denoting the body of the switch statement (also referred to as the case block).

    Inside these curly braces, we put all the individual case clauses of the switch, optionally also containing a default clause (typically at the end).

    Here's the syntax in terms of code:

    switch (expression) {
       case match1:
          statements;
       case match2:
          statements;
       /* ... */
       case matchN:
          statements;
       default:
          statements;
    }

    Learn more in JavaScript Conditions — switch.

  4. What is meant by the case block of a switch statement?

    The case block of a switch statement represents the entire code encapsulated inside the set of curly braces ({}). Learn more in JavaScript Conditions — switch.

  5. What is a case clause in a switch statement?

    A case clause, beginning with the case keyword, denotes a case to match against in a switch statement. Learn more in JavaScript Conditions — switch.

  6. A switch statement can have as many case statements inside of it as we want to. True or false?

    True.

  7. The pair of curly braces ({}) used while representing a switch statement in JavaScript is part of its syntax unlike the case with if and else where it denotes a block statement. True or false?

    Absolutely true. Contrary to what one might believe, the set of curly braces ({}) in a switch statement does NOT represent a block statement.

  8. If we wish to have multiple statements inside a given case clause, we always need to use a block statement. True or false?

    False, the entire set of statements following a case clause (after the :) are part of that clause up until the occurrence of another case clause or the end of the containing switch statement; there's no explicit need to use a block statement.

    Learn more in JavaScript Conditions — switch.

  9. What is fallthrough in a switch statement?

    Fallthrough is the name given to the mechanism whereby when execution enters a case clause, it tips over into the next clause automatically unless we explicitly command it not to (using the break keyword).

    Learn more in JavaScript Conditions — switch: What are fallthroughs?.

  10. What is the default clause meant for in a switch statement?

    The default clause is meant to denote some code to be executed in case none of the preceding cases matches. Learn more in JavaScript Conditions — switch: The default clause.

  11. Regardless of where we place a default clause within a switch statement, it's always executed last. True or false?

    True; even if default comes first inside a switch statement, it's evaluated in the end after evaluating all the case clauses. Learn more in JavaScript Conditions — switch: The default clause.

  12. Specify one scenario where the switch statement is not of any use to us.

    When we ought to compare a given value against a range instead of a concrete value. Or when we have only two cases to match for, one being the default case (when the previous one doesn't match) — in this case, we're better off at using if...else.

    Learn more in JavaScript Conditions — switch: When not to use switch?