Menu Close

Articles

JavaScript: hello

JavaScript was created by Brendan Eich in 1995. Java and JavaScript are completely different languages, both in concept and design. The official name for JavaScript is actually ECMA Script.

What is JavaScript?

JavaScript is a scripting language executed in Browser. It was designed as a light-weight language, to replace Java applets.  Now, JavaScript has grown in complexity and popularity. It is one of the top 3 languages a web developers should learn:

  1. HTML to define the content of web pages
  2. CSS to specify the layout of web pages
  3. JavaScript to program the behavior of web pages

This tutorial is only about JavaScript, but you should know some HTML to understand what’s going on.

Example:

This is an HTML fragment containing JavaScript code:

Comments: After <script> tag that belongs actually to XHTML you can observe the second and third and forth lines, separated by symbols:  /* … */. This is called a “block” comment.

Second form of comment is using two symbols: “//”. This is called single line comment, and the same symbol can be used to create end of line comments.

In JavaScript a line of code should end with semi-column. “;”. However many times this symbol is optional. JavaScript is forgiving, and will infer the end of statement for you.

Variables: Can you spot the variable declaration? It is done using let keyword followed by operator “=” and a string literal “Hello, Worlds!”, that is enclosed in double quotes. The variable is therefore a string in this case. Java do not require explicit type declarations.

JavaScript in Browser

JavaScript is working in any modern browser: Chrome, Firefox, Internet Explorer, Safari or Opera. It does not have it’s own user interface but can be embedded into HTML pages. Using JavaScript we refer to “document object” to write something into it using function: document.write().

JavaScript used to have no competition for running in browser, but situation was changed recently with the launch of Web Assembly language WASM (2015). This is an intermediate language that can be used by language designers to create high level languages for the browser.

JavaScript in Node

JavaScript can be run outside of web browser using Node.js virtual machine: Node JS Home. It is a runtime package built on top of  Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

Using Node we can create:

  • console based applications
  • back-end web server applications

Node service can run a HTTP server that can render and serve HTML pages. 

Learning JavaScript

To learn JavaScript language require significant effort. Despite initial design for a “tiny language” for browsers to manipulate HTML document, the Internet evolution has created pressure for the language to evolve. Now JavaScript compete in complexity with Java.

Syntax:

JavaScript syntax is using short English words, combination of words or abbreviations. JavaScript is free form, case sensitive language. Name of classes are usually starting with uppercase letters like: Array, Map, Set. Usually the keywords and function names are lowercase. 

Keywords:

abstract arguments await* boolean break byte case catch char class* const continue debugger default delete do double else enum* eval export* extends* false final finally float for function goto if implements import* in instanceof int interface let* long native new null package private protected public return short static super* switch synchronized this throw throws transient true try typeof var void volatile while with yield

Statements:

Java script is a structured language from Algol family, similar to C. It is a curly bracket language based on “statements”. A statement is usually starting with a keyword and contains identifiers, expressions, brackets and separators.

For example:

In the example above we show a special statement that is called: anonymous block. It start with separator “{” that marks the beginning of the block and it ends with separator: “}” that marks the end of the block. 

The block statement can encapsulate (contain) other statements. In the example there are two other statements. First is starting with “let” keyword, and contains two declarations for two “variables”. Second statement is a function call with expression argument: (a+b).

The second statement is “indented” using spaces. It is a good practice to align your code using spaces and avoid using tabs. Usually you can setup your editor to replace tabs with four spaces. JavaScript indentation is optional.

JavaScript Engine

JavaScript is running with help of “JavaScript Engine”. This is a product that can be embedded in different applications that are capable to run JavaScript.

Most popular engines:

Engine nameOrganizationNotes
V8GoogleUsed by many browsers and NodeJS.
RhinoMozillaWritten entirely in Java.
SpiderMonkeyNetscapeCurrently maintained by Mozilla organization
HermesFacebookHermes-powered apps launch faster

Run JavaScript

You can learn JavaScript in your browser or in console. You do not need a special IDE to run the examples presented in this tutorial. All you need is a browser that has support for JavaScript.

Usually one uses a text editor to create source files. Most popular editors are free editors for multiple programming languages. You should already have an editor to use if you have learned HTML+CSS. We recommend one of these editors:

How to run JavaScript in Mozilla Firefox:

Open a new tab in Firefox and in the URL bar, and enter about:blank to display a blank tab so that you can see what you are doing. From the Firefox menu select: Tools -> Web Developer -> Web Console. A new Web Console window will open. From the Web Console window click the JS button on the toolbar.

How to run JavaScript in Chrome:

Open Chrome, navigate to: ≡ View -> Developer -> JavaScript Console or More Tools -> JavaScript Console or press Ctrl + Shift + J. This will opens the JavaScript console where you can write and test your code. 

How to run JavaScript in NodeJS:

You can download NodeJS from here: download

  • Open command prompt,
  •  Change directory where your *.js examples are located,
  • Run command: like c:\program files\nodejs>node hello.js.

Note: We recommend to add “c:\program files\nodejs” to your $path so you can run node.

Read next: Arrays