JavaScript (Control Structures- Conditional) Flashcards
(12 cards)
are the building blocks of logic in
JavaScript. They allow programs to make decisions and
repeat actions based on conditions
Control structures
Control Structures
(Categories)
- Conditional (Decision-Making) Structures
- Loops
allow a program to evaluate a
condition (a Boolean expression) and execute specific blocks
of code based on whether the condition is true or false.
Conditional structures
- condition is a Boolean expression (evaluates to true or
false). - executes code only if a condition is true.
- If the condition is true, the code block inside the {}
runs. - If the condition is false, the code block is skipped
if statement
- Adds an alternative path when the condition is false.
- The if part checks a condition.
- If the condition is true, the code inside the if block runs.
- If the condition is false, the code inside the else block
runs instead.
If…else statemen
- JavaScript checks the conditions from top to bottom.
- As soon as it finds a condition that is true, it executes
that block and skips the rest. - If no condition is true, the code inside the else block (if
provided) runs.
if…else if…else
is another control flow
structure, similar to if…else if…else, but it’s often cleaner
and easier to read when you’re checking a single variable or
expression against multiple possible values.
* Used when you’re comparing one value against many cases.
* A cleaner way to handle multiple specific values of a single
variable
switch Statement
play a critical role
in
how expressions are evaluated in control flow
structures like if statements, loops, and ternary
operators. Understanding this concept is key to writing
efficient and bug-free code.
truthy and falsy values
is a value that, when coerced to a boolean,
results in false.
falsy value
is any value that is not falsy. In other
words, if a value is not one of the falsy values (listed
above), it is considered truthy.
truthy value
falsy value example
- false — the boolean value false.
- 0 — the number zero.
3.-0 — the negative zero (still falsy). - ”” (empty string) — an empty string is considered falsy.
- null — represents the absence of a value.
- undefined — represents an uninitialized or missing value.
- NaN — represents “Not-a-Number,” the result of an invalid
mathematical operation.
truthy value
- true — the boolean value true.
- Any non-zero number — e.g., 1, -1, 42, etc.
- Non-empty string — e.g., “hello”, “false”, “0”, “ “ (a
string with a space). - [] (empty array) — an empty array is truthy.
- {} (empty object) — an empty object is truthy.
- function() — any function is truthy.
- Infinity and -Infinity — both are truthy.
- new Date() — any valid Date object is truthy