JavaScript Language Basics

ECMAScript

ECMAScript is a language specification. It was created to standardize the scripting language used in browsers. There are (were) various implementations of ECMAScript, but JavaScript is the most well-known and most commonly used. Today ECMAScript and JavaScript are synonymous.

Script Elements

The script element is used to load inline code or reference executable code.  When used to load inline code, the actual executable code is included between the opening and closing tags.  When used to reference executable code, no content is included between the opening and closing tags and a src attribute is included to specify a URI to the external script file.

Script elements can be placed within the head element or within the body element.  However, if the executable code references one or more elements in the HTML document, it is crucial that the script code not be loaded until after the HTML elements have been declared in the body.  To ensure this, we often place script element that reference HTML elements directly before the closing body tag.

The example below demonstrates the use of a script elements to load inline code.

<head>
   ...
   <script>alert("testing")</script>
</head>

The example below demonstrates how to load code from an external file named validateForm.js located in a subdirectory named javascript.  We often place the <script> tag just before the closing <body> tag to ensure the browser has loaded the contents of the body into memory before the JavaScript is loaded and run.

<body>
   ...
   <script src="javascript/validateForm.js"></script>
</body>

Strict Mode

Strict mode makes the JavaScript engine throw errors. To run an entire file in strict mode, add the following as the first line in the file.

“use strict”;

To run a function in strict mode include the statement as the first line in the body of the function.

The following statements are not allowed in strict mode.

x = 3.14;            // undeclared variable
delete x;            // deleting variables

function x(p) { 
    ...
} 
delete x;            // deleting functions

function x(p, p) {   // using duplicate parameters
    ...
}

It is recommended that you use strict mode in your code.

Javascript Comments

JavaScript uses the same types of comments found in Java and C.

// single line comment
/* 
     multiline 
     comment
 */

Identifier Names

  • Can contain alphanumeric characters, underscore and dollar sign
  • Must start with alpha character, underscore or dollar sign.

Keywords

The following words are keyword in the JavaScript language, hence can not be used as identifiers.

  • break
  • case
  • catch
  • class
  • const
  • continue
  • debugger
  • default
  • delete
  • do
  • else
  • enum
  • export
  • extends
  • finally
  • for
  • function
  • if
  • import
  • in
  • instanceof
  • new
  • return
  • super
  • switch
  • this
  • throw
  • try
  • typeof
  • var
  • void
  • while
  • with
  • yield

The following are reserved in strict mode:

  • implements
  • interface
  • let
  • package
  • private
  • protected
  • public
  • static

Blocks and Statements

In JavaScript a block of code is formed using opening and closing curly braces { } and statements are terminated with a semicolon (;).

Like with Java, we can omit curly braces if the block consists of a single statement. It’s preferred, however, to use {} on all code blocks.

Logging Messages to a Console

All browser have a console that you can write messages to. The console is primarily used to debug code as the user doesn’t see what is printed on the console when visiting a web page.  Each of the following functions allow you to write a string to the Javascript console.

console.log(message);    // log a general message - available in all browsers
console.error(message)   // log an error
console.info(message);   // log an information message
console.warn(message);   // log a warning message

© 2018 – 2021, Eric. All rights reserved.