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…
In recent JavaScript studies, I discovered several concepts that I had previously encountered but lacked a complete understanding of: ternary, switch, break, and continue. Upon attempting them with simple examples, these ideas became clearer. The ternary operator functions as a condensed version of the if-else statement. For instance: let result = age = 18 ?
Adult : Minor; It proves beneficial when the condition is uncomplicated and the goal is to maintain succinct code. Switch statements come in handy when one value needs to be compared against multiple options. For example, checking a day of the week: switch (day) { case Monday: console.log(Start); } Additional cases can be included based on specific requirements.
Break is utilized when the objective is to terminate a loop. For example: if (i === 5) break; This causes the loop to stop if the condition is true. Continue differs from break in that it skips the present iteration while permitting the loop to continue running. if (i === 5) continue; In my comprehension, Break stops the loop, Continue skips the current iteration, Ternary offers a compact alternative to if-else, and Switch enables the handling of different cases.
Though these concepts may appear minor, grasping them enhances the ease of working with JavaScript. I am still in the learning phase; however, I have found that creating small examples is more advantageous than merely reading the syntax.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.