So you think you can JavaScript? Part 1

Karim Alibhai
Hello, JavaScript
Published in
5 min readDec 28, 2016

Everyone’s talking about it. JavaScript is the new C. Whatever that means. So you decide to pick it up. You think you’ve done Java, so how different can it be? After all, the names are pretty much the same.

Java is to JavaScript as Car is to Carpet.

That that’s all they have in common. The first four letters.

Okay so it’s more than that. But it’s not because one is based on the other. Java and JavaScript share a very similar syntax. This is because both languages have what is known as C-style syntax.

Side note: JavaScript was originally named LiveScript and was made by Brendan Eich at Netscape. In order to compete with Microsoft’s Visual Basic Script, Netscape made a deal with Sun to be able to use the Java trademark in the name of their language in order to trick developers into pursuing the language. It worked.

C-style syntax languages are a family of languages that all have syntax based on the syntax of the C language. Typically, this syntax applies to the basic control flow of the language (i.e. the general structure of the if-statement, the structure of a for-loop).

It also gives us some bonus stability because these languages are not whitespace-sensitive. This means that it doesn’t matter how many tabs or spaces you use, the code will work the same. Whitespace-sensitive languages are for kids. You’re in the major leagues now.

Side note: Let’s also talk about the appropriate abbreviation for JavaScript. When you shorten it, it’s JS and not JScript. JScript is what Microsoft called their implementation of JavaScript in Internet Explorer after they surrendered to the fact that VBScript always sucked. It was a way for them to avoid the issue of the ‘Java’ trademark.

So now you know the syntax and how unlike Java it is. But you can’t for the life of you find where to download the JavaScript compiler. You find a few that claim to obfuscate the code but the end result just looks like a messy version of the code you wrote. Not what you expected. Maybe you’re missing something…

You are. There’s no compiler.

Let’s zoom into the computer and look at why.

The computer is basically this big system of switches. Switches can either be on or off. Therefore, computers operate in binary. But humans do not.

Humans need more intuitive language constructs to create sophisticated systems. So we created assembly. It does all the basic operations we need, but without us writing in binary.

But we’re still faced with the same problem. The computer doesn’t just start understanding assembly because it’s more convenient for us. This is where compilers come in.

A compiler translates code from one language to another. In the case of assembly, we translate from assembly to binary. Now we can write in assembly and run in binary. Sweet.

But now assembly is boring. We need something easier and more powerful. So then we made C. Now here’s the thing: we’ve already got a compiler that goes from assembly to binary. We can leverage the power of this compiler and of the assembly language by compiling C to assembly and then running the assembly compiler.

That’s what a compiler does. But now let’s say that we want to completely avoid compiling code. We just want to run it right away. How can we achieve this?

Well the computer still only understands binary. Let’s say that we create a program that can do math. We would have to compile this program for it to run and so we do. But instead of telling the program to do some predefined math, we tell it to do the math that’s in some file.

By doing so, we’ve given the ability of math to this file without the need of translating the file into binary. We can do this at a more complex scale where we can do full operations through writing things in this file. We can create an entirely new language that is run live by executing the actual operations within a program that is compiled.

These languages are known as interpreted languages. They are never compiled but rather run live. The obvious caveat of an interpreted language is that the system must have the interpreter (the compiled software that executes the operations).

JavaScript is an interpreted language. The interpreter of JavaScript is built into the web browser. The web browser must be a compiled software and lends its capabilities to JavaScript when it interprets a JavaScript program.

Cool. So Java is a compiled language and JavaScript is interpreted. I get it now.

No, you don’t. Java isn’t a compiled language. It’s a mix of both. Running a Java program requires two steps. The first step is when the Java code is compiled. This is the step that makes you think that Java is a compiled language since it gets compiled but you’re wrong. The end result is not binary.

It is a higher-level language that is a type of bytecode (which is a series of instructions that are similar to assembly-language instructions but completely and utterly different). Once you have compiled your Java code, you then pass the obtained bytecode to the Java Virtual Machine (JVM) where it is interpreted as if it were an interpreted language. The following is a simulation of how the JVM runs a simple Hello World program.

The JVM at its finest.

Alright, so we’ve established what JavaScript does not do and that we can write it without a compiler. But what does it actually do? Why do we need it? Can I write my Hello World program now?

No. In the next part of the series, we’ll talk about more things.

--

--