As with any language, there are ways to write JavaScript and better ways to write it. What is not as obvious in other languages, however, is the direct implications of your code for the user experience of a website. Complicated, inefficient code can clog up a browser, eat CPU cycles, and, in some cases, even crash the browser.
Take a look at this simple four-line snippet by Talon Bragg from https://hackernoon.com/crashing-the-browser-7d540beb0478:
txt = "a";
while (1) {
txt = txt += "a"; // add as much as the browser can handle
}
Warning: do not attempt to run this in a browser! If you're curious about what this does, it will eventually create an out-of-memory exception in the browser that will kill the tab with a message that the page has become unresponsive. Why is this? Our while loop has a simple truthy value for its condition, so it will continue adding "a" to the string text until the memory...