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 (A)</p> Signup and view all the answers

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

<p>NaN is a falsy value. (C)</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! (B)</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. (D)</p> Signup and view all the answers

Which of the following is NOT considered a falsy value?

<p>'0' (C)</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. (B)</p> Signup and view all the answers

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

<p>1 (D)</p> Signup and view all the answers

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

<p>true (A)</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. (D)</p> Signup and view all the answers

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

<p>false (A)</p> Signup and view all the answers

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

<p>Boolean (C)</p> Signup and view all the answers

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

<p>false (B)</p> Signup and view all the answers

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

<p>AND (D)</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. (B)</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. (C)</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... (A)</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 (C)</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. (B)</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. (C)</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. (B)</p> Signup and view all the answers

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

<p>true (A)</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. (B)</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. (C)</p> Signup and view all the answers

Flashcards

Comparison operators

Special symbols that compare values and return a boolean value (true or false).

Boolean type

Data type representing truth values; can be either 'true' or 'false'.

if-else statement

A conditional statement that executes code based on whether a condition is true or false.

switch statement

A conditional statement that selects code to execute based on the value of an expression.

Signup and view all the flashcards

Logical operators (AND, OR, NOT)

Operators combining or modifying boolean expressions (e.g., AND returns true if both expressions are true).

Signup and view all the flashcards

Boolean comparison of strings

Strings are compared character by character until a difference is found; a longer string is greater than a shorter one with same prefix characters.

Signup and view all the flashcards

Output of "3 > 2 > 1"

Evaluates to 'true'.

Signup and view all the flashcards

if statement

Conditional statement that executes a block of code if a specified condition is true.

Signup and view all the flashcards

if...else

Makes a decision based on a condition. If the condition is true, one block of code executes; otherwise, another block executes.

Signup and view all the flashcards

if...else if

Allows multiple conditions to be tested sequentially. This is used when you need to handle multiple cases.

Signup and view all the flashcards

Conditional Operator

Shorthand way to write an if...else statement. Syntax is condition ? valueIfTrue : valueIfFalse

Signup and view all the flashcards

Logical Operators

Operators that combine or modify conditions. Includes && (AND), || (OR), and ! (NOT).

Signup and view all the flashcards

Logical OR (||)

Returns true if at least one operand is true; otherwise, it returns false. Use this in tests requiring either condition to meet.

Signup and view all the flashcards

Logical AND (&&)

Returns true only if both operands are true; otherwise it returns false. Used when both conditions must be met

Signup and view all the flashcards

if statement

Specifies a block of code to be executed if a certain condition is true

Signup and view all the flashcards

prompt()

Displays a dialog box to the user and gets input from them.

Signup and view all the flashcards

Falsy Value

A value that evaluates to false in a Boolean context.

Signup and view all the flashcards

switch statement

A conditional statement that executes different blocks of code based on the value of an expression.

Signup and view all the flashcards

switch case

A specific block of code within a switch statement that's executed when a matching expression is found.

Signup and view all the flashcards

break statement (switch)

A statement that exits a switch statement immediately, preventing further execution of subsequent cases.

Signup and view all the flashcards

default case (switch)

A block of code in a switch statement that is executed if no other case matches the expression.

Signup and view all the flashcards

Falsy Values (in JS)

Values that evaluate to false in a boolean context in JavaScript.

Signup and view all the flashcards

Logical OR (||)

Returns true if at least one operand is true; otherwise, returns false.

Signup and view all the flashcards

Empty String in Boolean Context

Evaluates to false when used within a if or similar condition.

Signup and view all the flashcards

Short-circuiting in Logical OR

If the first operand evaluates to true, the evaluation stops and true is returned; otherwise, the second operand is evaluated.

Signup and view all the flashcards

Variable defined via ||

Checks for a variable's value. If the variable is falsy, a default value is used.

Signup and view all the flashcards

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