DEV Community

Karthick Karthick
Karthick Karthick

Posted on

Javascript Important Operator, Statement and Concept

Ternary Operator in JavaScript
What is it?

The ternary operator is a shortcut for if...else.
It is called “ternary” because it uses 3 parts:
Copy code

condition ? value_if_true : value_if_false
**Why do we need it?
Js
Example
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");

Js
Copy code
console.log(age >= 18 ? "Adult" : "Minor");**
It makes code shorter and cleaner.
Syntax
Js
Copy code
condition ? expression1 : expression2;

  • If condition is true → expression1 runs

  • If condition is false → expression2 runs

** When was it invented?**
The ternary operator did not start in JavaScript.
It first appeared in the C programming language created by
Dennis Ritchie in July 17th 1972.
JavaScript was created later in 1995 by
Brendan Eich
.
JavaScript adopted many ideas from C, including the ternary operator.

Truthy and Falsy Values
What does Truthy mean?
In JavaScript, some values behave like true even if they are not exactly true.
Example:
Js
Copy code
if ("hello") {
console.log("This runs!");
}
Why? Because "hello" is truthy.
Falsy Values (Only 6)

**- These are considered FALSE:

  • Js
  • Copy code
  • false
  • 0
  • ""
  • null
  • undefined
  • NaN**

Why do we need truthy/falsy?
It makes checking values easier.
Instead of:
Js
Copy code
if (name !== "" && name !== null && name !== undefined)
We just write:
Js
Copy code
if (name)
Much simpler

Switch Statement

What is it?
switch is used when you want to compare one value with many possible cases.
Instead of writing many if...else if statements.
Syntax
Js
Copy code
**switch (expression) {
case value1:
// code
break;

case value2:
// code
break;

default:
// if nothing matches
}**
Why is break important?
If you remove break, it continues executing next cases.
When was switch invented?
switch also came from the C language (July 17th 1972) by
Dennis Ritchie.
JavaScript (1995) reused it.

Why Were These Invented?
Programming needed:

  • Faster decision making
  • Cleaner readable code
  • Alternative to long if-else chains
  • Better performance in some cases So languages like C introduced: switch ?: (ternary) Later JavaScript adopted them.Ternary Operator in JavaScript What is it? The ternary operator is a shortcut for if...else. It is called “ternary” because it uses 3 parts: Copy code

condition ? value_if_true : value_if_false
Why do we need it?
Instead of writing:
Js
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
You can write:
Js
Copy code
console.log(age >= 18 ? "Adult" : "Minor");

Syntax
Js
Copy code
condition ? expression1 : expression2;
If condition is true → expression1 runs
If condition is false → expression2 runs
Example
Js
let marks = 40;

let result = marks >= 35 ? "Pass" : "Fail";

console.log(result);
Output:
Pass
When was it invented?
The ternary operator did not start in JavaScript.
It first appeared in the C programming language created by
Dennis Ritchie in July 17th 1972.
JavaScript was created later in 1995 by
Brendan Eich.
JavaScript adopted many ideas from C, including the ternary operator.
Truthy and Falsy Values
What does Truthy mean?
In JavaScript, some values behave like true even if they are not exactly true.
**
Falsy Values** (Only 6)
These are considered FALSE:
Js
Copy code
**

  • false
  • 0
  • ""
  • null
  • undef **ined NaN Everything else is truthy.

**Why do we need **truthy/falsy?
It makes checking values easier.
Instead of:
Js
Copy code
if (name !== "" && name !== null && name !== undefined)
We just write:
Js
if (name)
Much simpler
Switch Statement
What is it?
switch is used when you want to compare one value with many possible cases.
Instead of writing many if...else if statements.
Syntax
Js
Copy code
switch (expression) {
case value1:
// code
break;

case value2:
// code
break;

default:
// if nothing matches
}
Why is break important?
If you remove break, it continues executing next cases.
This is called fall-through.
When was switch invented?
switch also came from the C language (July 17th 1972) by
Dennis Ritchie.
JavaScript (1995) reused it.
Why Were These Invented?
Programming needed:
Faster decision making
Cleaner readable code
Alternative to long if-else chains
Better performance in some cases
So languages like C introduced:
switch
?: (ternary)
Later JavaScript adopted them.

Top comments (1)

Collapse
 
buddingdeveloper profile image
Ebenezer

Simple and Good Buddy!