JavaScript is a wildly popular interpreted scripting language that in early 2019 became the language most frequently learned by developers. JavaScript is an open standard, not controlled by any single vendor, with numerous implementations and an easy-to-learn syntax that makes it popular with beginners and veteran developers alike.
JavaScript dates from the earliest days of the World Wide Web. The language was first rolled out as a way to add lightweight client-side functionality to Web pages, and is widely used for that purpose today. Just about anything interactive or animated on a Web page today is rendered in JavaScript, including basically the entire ecosystems of online advertising and metrics. But JavaScript doesn’t just run in the browser. Thanks to development frameworks such as Node.js, JavaScript is now used to write code for just about any niche you can think of, from clients to servers to the cloud.
JavaScript defined: What is a scripting language, and how are Java and JavaScript different?
As its name implies, JavaScript is a scripting language. Traditional languages such as C++ are compiled before they’re run into executable binary form, with the compiler checking for any errors in the entire program before the process is complete. Scripting languages, by contrast, are executed one line at a time by another program called an interpreter. Scripting languages got their start as simple series of shell commands used to execute other programs, but their flexibility and ease of use made them a popular type of programming language in their own right, and they particularly became important with the rise of the Web.
JavaScript itself arose in those early days of the Web, and its history explains the somewhat anomalous Java part of its name. In 1995, Netscape had just signed a deal with Sun Microsystems to become the first licensee of Sun’s Java language, gaining the ability to run Java applets in the pioneering and then-dominant Netscape Navigator Web browser. But some within the company believed that supporting a more lightweight scripting language in Navigator was also important. Brendan Eich, a Netscape employee at the time, explained:
“There were people who argued strongly that Java’s fine for programmers who build components, but there’s a much larger audience of people who write scripts or maybe copy a script from somebody else and tweak it. These people are less specialized and may be paid to do something other than programming, like administer a network, and they write scripts part-time or on the side. If they’re writing small pieces of code, they just want to get their code done with the minimal amount of fuss.”
Eich was prescient: Java applets never really took off, whereas the scripting language he created (very quickly) for Netscape remains the backbone of interactive websites. Originally called LiveScript, the language was created with syntax that was superficially similar to Java in many ways in order to tap into the Java developer community, though in fact there is no direct relationship between the two languages. Still, because Netscape already had a deal with Sun, just before its release the language was rebranded as JavaScript and billed by the two companies as a “complement” to the Java language.
In 1997, the European Computer Manufacturers Association (ECMA) issued a standard defining the language that anyone could implement independent of Netscape; because “Java” remained a Sun trademark that only Netscape had a license to use, this standardized version was dubbed “ECMAScript.” Microsoft had referred to the version it implemented in Internet Explorer as “JScript.” However, these names long ago fell out of general use, although ECMAScript remains the official name of the standard, which has been revised numerous times over the years to keep up with improved processing power and Internet bandwidth. In practice, everyone refers to the language as JavaScript. Technically, only the Mozilla Foundation (which took over Netscape’s intellectual property in 2003) has official approval from Oracle (which acquired Sun in 2010) to use the Java trademark, but Oracle hasn’t made any effort to prevent the universal use of the name.
Enable JavaScript
Back in the early days of JavaScript, it wasn’t uncommon for browsers to have JavaScript execution turned off by default. Today, that would leave most of the Web unusable, since JavaScript is an integral part of just about any professionally designed Web page. If for some reason you think JavaScript has been disabled in your browser, this is a great page that gives step-by-step instructions for enabling it. (You can also reverse these instructions to disable JavaScript, to give you a sense of how bleak the world would be without it.)
Download JavaScript
What if you want to play around with JavaScript today? Well, because it’s an interpreted language, there’s really nothing you need to download. You can fire up your favorite text editor and start typing code! There’s a JavaScript interpreter built in to any browser you could name that can execute commands, so you can simply embed your JavaScript into an HTML document and load it up to test.
If you want to run JavaScript code in a desktop environment—which will, among other things, give you access to the filesystem, since JavaScript running in the browser is deliberately sandboxed for security purposes—then you can download Node.js.
JavaScript editor
Of course, most developers aren’t going to simply type code by hand into a text file. We’ve come to accept software assistance in creating our software. When it comes to JavaScript, InfoWorld has you covered: We put together a list of the 10 best JavaScript editors available. These range from Sublime Text, a text editor with syntax highlighting and multiple windows to help you navigate around your files, to Visual Studio Code, a full IDE from Microsoft. Many of the options we discuss are open source and free.
JavaScript syntax: the basics
What goes into a JavaScript application? Let’s take a look at some of the fundamental components of JavaScript syntax, with links to the W3Schools JavaScript Reference where you can find more details:
- JavaScript operator: These are the building blocks of program functionality. They’re the basic arithmetic operators from which you’d build mathematic functions and assignment operators that allow you to set the value of variables.
- JavaScript function: A function is a block of self-contained code that you write to perform a specific task. Some are built into the language, while others you can write yourself to carry out the logic of your application. Once you’ve defined it, you can call the function—invoke it and optionally pass in some variables or data for it to work on—elsewhere in your program.
- JavaScript substring(): A method that extracts characters that you specify from within a string and outputs a new string consisting of only those characters.
- JavaScript array: A special kind of variable that can hold a whole list of values at once. JavaScript has a number of tools for finding and manipulating the specific values you want within an array. For instance…
- JavaScript forEach(): This method calls a specified function once for each element in an array, in order.
- JavaScript map(): Something of a variation on
forEach()
, a map creates a new array containing the results of calling a function on every value within another array. For instance, you could multiply every value in another array by 10.
Learn JavaScript with JavaScript tutorials
Obviously if you want to get serious about becoming a JavaScript developer you’ll want to go deeper. The W3Schools reference is great for diving into details on individual pieces of JavaScript syntax, but there are other, more systematic tutorials:
- The Modern JavaScript Tutorial is a very comprehensive resource that takes you step by step from a simple Hello, World! program to ways to build sophisticated interactive functionality into the browser.
- JavaScript Examples at Tutorial Republic offer a wealth of short JavaScript programs that escalate in difficulty so you can see how JavaScript code works in action.
- Jonathan Freeman’s JavaScript Everywhere column on InfoWorld tackles real-world problems and offers tutorials on how JavaScript can solve them.
JavaScript Snake is a great specific example of what you can do with JavaScript. This is a classic simple game that can be built so as to run in the browser using only JavaScript, HTML, and CSS. This tutorial from Panayiotis Nicolaou shows you how.
JavaScript interview questions
Because JavaScript is so widely popular, many hiring managers expect candidates to know how to write it, and often ask candidates to write JavaScript code on the spot, at the interview. If you’re trying to prepare for an interview like that, you can find plenty of lists of sample questions online—Toptal has a good one, for instance—but for a more holistic approach, we recommend this essay from Naren Yellavula on dev.bits(). It’s a mini JavaScript tutorial in its own right, but it specifically focuses on the building blocks you need in order to succeed at the sorts of questions that tend to arise at interviews. Take the time to read it and best of luck!