2. IF JavaScript
26 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What will be logged to the console when age is set to 0 and evaluated with the condition 'if(age){ ... }'?

  • The code will not run.
  • Variable is defined.
  • Variable has NOT been defined. (correct)
  • An error will occur.
  • In the expression 'let myCity = city || defaultCity;', what value will myCity have if city is undefined?

  • null
  • Vilnius (correct)
  • Kaunas
  • undefined
  • What does the statement 'console.log(false || true);' evaluate to?

  • undefined
  • true (correct)
  • null
  • false
  • When logically evaluating 'console.log("abc" || "");', what will be the output?

    <p>abc</p> Signup and view all the answers

    Which of the following statements about the value NaN is true?

    <p>NaN is a falsy value.</p> Signup and view all the answers

    What will the output be if the value of 'a' is 2 + 2 in the provided switch statement?

    <p>Exactly!</p> Signup and view all the answers

    Which statement about the use of 'break' in the switch statement is true?

    <p>It stops the execution of the switch after a matching case.</p> Signup and view all the answers

    Which of the following is NOT considered a falsy value?

    <p>'0'</p> Signup and view all the answers

    What will happen if there is no break statement after case 4 in the switch?

    <p>The next case will be executed consecutively.</p> Signup and view all the answers

    In a Boolean context, which type of value is treated as true?

    <p>1</p> Signup and view all the answers

    What is the result of the comparison operation alert(2 > 1)?

    <p>true</p> Signup and view all the answers

    When comparing two strings, what is the first step in the algorithm?

    <p>Compare the first character of both strings.</p> Signup and view all the answers

    What boolean value is returned when executing alert(2 == 1)?

    <p>false</p> Signup and view all the answers

    In the statement if (year == 2022), what type of value does the condition evaluate?

    <p>Boolean</p> Signup and view all the answers

    What does the expression console.log(3 > 2 > 1) return?

    <p>false</p> Signup and view all the answers

    Which logical operator would return true only if both operands are true?

    <p>AND</p> Signup and view all the answers

    Why is the comparison between two strings done character by character?

    <p>To find the first differing character.</p> Signup and view all the answers

    Which of the following statements is true regarding the boolean values in JavaScript?

    <p>Both true and false can be represented as strings.</p> Signup and view all the answers

    What will the alert display if the user inputs '2014' in the if-else statement checking for the year 2015?

    <p>Too early...</p> Signup and view all the answers

    In the conditional operator example, what does the expression 'age > 18 ? true : false' evaluate to if age is 20?

    <p>true</p> Signup and view all the answers

    When using AND (&&), what is the outcome if one operand is false?

    <p>The entire expression is false.</p> Signup and view all the answers

    What will happen if 'hour' is set to 20 and the condition 'if (hour < 10 || hour > 18)' is evaluated?

    <p>The office is closed.</p> Signup and view all the answers

    What does the else if clause allow you to do in an if-else structure?

    <p>Specify multiple conditions in a single if statement.</p> Signup and view all the answers

    What will the alert display if the variable accessAllowed is set to true?

    <p>true</p> Signup and view all the answers

    In the statement 'alert(accessAllowed)', what information is conveyed when accessAllowed is false?

    <p>The user is denied access.</p> Signup and view all the answers

    What will occur in an if statement using logical NOT (!) before a variable that is truthy?

    <p>The condition will evaluate to false.</p> Signup and view all the answers

    Study Notes

    JavaScript Fundamentals

    • JavaScript is a programming language, used to create interactive web pages
    • Comparison operators allow comparing values
    • == (equal to) checks only value
    • === (equal in value and type) checks value and type
    • != (not equal to) checks only value
    • !== (not equal in value or type) checks value and type
    • > (greater than)
    • < (less than)
    • >= (greater than or equal to)
    • <= (less than or equal to)

    Boolean Types

    • Boolean values represent logical states
    • true or false

    Conditional Statements

    • if-else statements control program flow, based on conditions
    • If the condition result is true the block of code is executed
    • switch statements handle multiple conditions
    • The switch statement checks if a given variable or expression equals a case value and then executes the statements associated with that case. If no matching case is found, the optional default statements may be executed.

    Logical Operators

    • Logical operators combine conditions using Boolean logic to evaluate to true or false
    • || (OR): Returns true if at least one operand is true
    • && (AND): Returns true if both operands are true
    • ! (NOT): Negates a Boolean expression (e.g., !true is false)

    Conditional Operator (Ternary Operator)

    • The conditional operator '? :' provides a compact way to express an if-else statement
    • condition ? expression1 : expression2

    Falsy Values

    • Certain values in JavaScript are considered false when used in a Boolean context
    • Examples include false, null, undefined, 0, -0, NaN, '' (empty string)
    • if statement conditional tests if a variable is defined or not

    Functions

    • Functions are reusable blocks of code, accepting parameters and returning values
    • Parameters are values passed to a function (inputs)
    • Functions perform actions via their function body
    • Functions may return a value to the function's caller

    Practical examples

    • Various example code snippets showing practical applications of these concepts are provided in slides (e.g. using prompt, alert, and console.log).

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Description

    This quiz covers the foundational concepts of JavaScript, including comparison and logical operators, boolean types, and conditional statements. Test your understanding of these essential programming principles that allow you to create interactive web applications.

    More Like This

    Code Error Detection and Correction Challenge
    5 questions
    JavaScript Programming Quiz
    5 questions
    Conditional Statements in JavaScript
    10 questions
    Use Quizgecko on...
    Browser
    Browser