The Syntax of JavaScript - JavaScript: Academy: The Stress Free Way to Learning JavaScript Inside & Out (2016)

JavaScript: Academy: The Stress Free Way to Learning JavaScript Inside & Out (2016)

Chapter 2. The Syntax of JavaScript

You may implement this language by placing JavaScript statements inside your webpage’s <script> tags. In general, you can put these <script>tags anywhere in the webpage. Professional website developers, however, usually keep these tags inside the page’s <head> tags.

Basically, a <script> tag instructs browsers to interpret the text it contains as a script. Here is the basic syntax of JavaScript:

Script tags accept the following attributes:

· Language– This attribute indicates the language being used. Recent versions of HTML and XHTML, however, no longer support this attribute.

· Type– This is the attribute being used in modern sites and webpages. You must use it to indicate the computer language you are using. Make sure that its value is“text/javascript”.

Taking those attributes into account, the syntax of JavaScript is:

Using JavaScript for the First Time

In this section, you’ll be using this scripting language for the first time. The code that you will use is designed to display“Hello World” on the screen of your computer. As you can see, the JavaScript code (see the screenshot below) involves an optional HTML element. That HTML comment allows you to save the code from browsers that don’t support the JavaScript language.

The symbol“//→” terminates the HTML comment. In JavaScript,“//” represents comments. That means you have to use a pair of slashes to prevent browsers from interpreting HTML comments as part of your JavaScript statements. Then, you must invoke the document.write function to write a string into your HTML file.

Important Note: You can use the document.write function to write HTML statements, text-based content, or both.

Here’s the code that you must type into your JavaScript editor:

If written and executed properly, the code given above will show you: Hello World!

Whitespaces and Line Breaks

This scripting language ignores tabs, spaces, and newlines inside your programs. That means you may use these elements to make your JavaScript codes readable and understandable.

Semicolons

Similar to other computer languages (e.g. C++), JavaScript requires you to terminate simple statements using a semicolon. However, you won’t have to add a semicolon if your statements are entered on separate lines. For instance, the code below works fine even if it doesn’t have any semicolon:

The Case-Sensitivity of JavaScript Statements

This programming language is case-sensitive. For example,“WORD,” “Word,” and“word” are treated as three different entities in JavaScript. That means you must be consistent with letter capitalization when working on functions, keywords, variables, and other identifiers.

JavaScript Comments

You must remember the following rules when creating a comment in JavaScript:

· This language ignores all of the text placed between the“//” symbol and the end of a line.

· You may use /* and */ to create comments. In general, programmers employ these symbols when creating multi-line comments.

· JavaScript also allows you to use <!— (i.e. the symbol used in HTML to start comments) when writing single-line comments.

· JavaScript doesn’t recognize → (i.e. the symbol used in HTML to end comments). You must use //→ instead.