Sponsored Content

DEV Community

Chandru
Chandru

Posted on

4 JavaScript Things I Learned Recently

While learning JavaScript, I came across a few things that I had seen before but didn't fully understand: ternary, switch, break, and continue.

After trying them with small examples, they started making more sense.

Ternary

The ternary operator is basically a short way of writing if-else.

For example:

let result = age >= 18 ? "Adult" : "Minor";

It is useful when the condition is simple and you want to keep the code short.

Switch

Switch is useful when you need to check one value against different options.

For example, if we want to check the day:

switch (day) {
case "Monday": console.log("Start");
}

We can add more cases depending on what we need.

Break

Break is used when we want to stop a loop.

For example:

if (i === 5) break;

When the condition is true, the loop stops there.

Continue

Continue is different from break.

It skips the current iteration but allows the loop to keep running.

if (i === 5) continue;

So, the way I remember them is:

Ternary → shorter if-else

Switch → different cases

Break → stop

Continue → skip and move on

These are small things, but understanding them makes JavaScript a little easier to work with.

I'm still learning, but I've found that writing small examples is much better than just reading about the syntax.

Top comments (0)