DEV Community


Discussion on: Finally Understanding JavaScript

ericgeek profile image

If-then-else? How about picking up another technique, the ternary operator?

while (i < 4) {
let message = (i ==3) ? "Happy Birthday dear " + name + "," : "Happy Birthday to you.
i = i + 1;

Which isn't the end of ways to refactor this. You could get rid of the temporary variable by having the ternary inside the document.write(), and you could change the while loop to a for loop. There's almost never only one way to do something when it comes to programming.

lynnecodes profile image
Lynne Author

This is great! Thanks for building on this code and showing me another way to write it! I'm learning so much.