Introduction

JavaScript is a cross-platform, object-oriented scripting language. It is a small and lightweight language. Inside a host environment (e.g. web browser), JavaScript can be connected to the objects of its environment to provide programmatic control over them.

JavaScript contains a standard library of objects, such as Array, Date and Math, and a core set of language elements such as operators, control structures, and statements. Core JavaScript can be extended for a variety of purposes by supplementing it with additional objects:

JavaScript and Java

JavaScript and Java are similar in some ways but fundamentally different in some others. The JavaScript language resembles Java but does not have Java's static typing and strong type checking. JavaScript follows most Java expression syntax, naming conventions and basic control flow constructs which was the reason it was renamed from LiveScript to JavaScript.

In contrast to Java's compile-time system of classes built by declarations, JavaScript supports a runtime system based on a small number of data-types representing numeric, Boolean, and string values. JavaScript has a prototype-based object model instead of the more common class-based object model. The prototype-based model provides dynamic inheritance; that is, what is inherited can vary for individual objects. JavaScript also supports functions without any special declarative requirements. Functions can be properties of objects, executing as loosely typed methods.

JavaScript is a very free-form language compared to Java. You do not have to declare all variables, classes and methods. You do not have to be concerned with whether methods are public, private, or protected, and you do not have to implement interfaces. Variables, parameters, and function return types are not explicitly typed.

Hello World

To open a JavaScript console inside of the "Chrome" web browser, press "Ctrl + Shift + J". Alternatively, it can be opened from the main settings dropdown by selecting "More tools" => "Developer tools", and then clicking on "Console" in the top navigation bar. Type the following into the console:

    
    function greetMe(yourName) {
      alert("Hello " + yourName);
    }
    

This defines a function which will generate a small "alert" pop-up in your browser. To invoke the function, type into the console:

    
    greetMe("World");
    

Variables

Variables are used as symbolic names for values in your application. The names of variables, called identifiers, must conform to certain rules. A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Identifiers are case-sensitive.

Declaring Variables

Variables can be declared in three ways:

  1. With the keyword "var":
            
        var x = 42;
            
    This syntax can be used to declare both local and global variables.

  2. By simply assigning it a value:
            
        x = 42;
            
    This always declares a global variable. This syntax is not recommended.

  3. With the keyword "let":
            
        let y = 13;
            
    This syntax can be used to declare a block scope local variable (see below).

Variable Scope

When you declare a variable outside of any function, is it a global variable, because it is available to any other code in the current document. When you declare a variable within a function, it is a local variable, because it is available only within that function.

JavaScript before "ECMAScript 2015" does not have block statement scope; rather, a variable declared within a block is local to the function (or global scope) that the block resides within. For example the following code will log "5", because the scope of x is the function (or global context) within which x is declared. It is not the block (enclosed by "{}"), which in this case is an if statement.

    
    // will print 5
    if (true) {
      var x = 5;
    }
    console.log(x);
    

This behaviour changes when using the "let" declaration; this declares a block scope local variable:

    
    // ReferenceError:
    // y is not defined
    if (true) {
      let y = 5;
    }
    console.log(y);
    

Global Variables

Global variables are properties of the global object. In web pages the global object is "window", so you can set and access global variables using the "window.variable" syntax.

Constants

You can create a read-only, named constant with the "const" keyword. The syntax of a constant identifier is the same as for a variable identifier.

    
    const PI = 3.14;
    

A constant can not change value through assignment or be re-declared while the script is running. It must be initialised to a value. The scope rules for constants are the same as for "let" block scope variables. If the "const" keyword is omitted, the identifier is assumed to represent a variable. You can not declare a constant with the same name as a function or variable in the same scope.

Data Types

There are seven data types:

Although these data types are a relatively small amount, they enable you to perform useful functions with your application. Objects and functions are the other fundamental elements in the language. Objects are named containers for values, and functions are procedures that your application can perform.

If...Else Statement

Use the if statement to execute a statement if a logical condition is true. Use the optional else clause to execute a statement if the condition is false.

    
    if (condition) {
      statement_1;
      statement_2;
      ...
    }
    else {
      statement_3;
      statement_4;
      ...
    }
    

The statements in the corresponding block will execute depending on the evaluation of the logical condition. The condition can be any expression that evaluates to true or false. You may also compound the statements using else if to have multiple conditions tested in sequence:

    
    if (condition_1) {
      statement_1;
      ...
    }
    else if (condition_2) {
      statement_2;
      ...
    }
    else if (condition_n) {
      statement_n;
      ...
    }
    else {
      statement_last;
      ...
    }
    

In the case of multiple conditions only the first logical condition which evaluates to true will be executed.

While Statement

A while statement executes its block of statements as long as a specified condition evaluates to true.

    
    while (condition) {
      statement_1;
      statement_2;
      ...
    }
    

The condition test occurs before the block in the loop is executed. If the condition returns true, the block is executed and the condition is tested again. If the condition returns false, loop execution terminates and control passes to the next statement following the loop. The exit condition must be reachable (i.e. must eventually evaluate to false) in order to avoid infinite looping.

Function Declarations

A function definition (also called a function declaration) consists of the "function" keyword followed by:

The following defines a simple function to add two numbers:

    
    function add(a, b) {
      return a + b;
    }
    

The function takes two arguments: a and b. The function block consists of a single return statement, which specifies the value returned by the function. Primitive parameters (such as a number) are passed to functions by value; the value is passed to the function but if the function changes the value of the parameter, this change is not reflected globally.

Reference