JavaScript is an object-oriented scripting language used to enable programmatic access to objects within both the client application and other applications. It is primarily used in the form of client-side JavaScript, implemented as an integrated component of the web browser, allowing the development of enhanced user interfaces and dynamic websites. JavaScript is a dialect of the ECMAScript standard and is characterized as a dynamic, weakly typed, prototype-based language with first-class functions. JavaScript was influenced by many languages and was designed to look like Java, but to be easier for non-programmers to work with.

History and naming

JavaScript was originally developed by Brendan Eich of Netscape under the name Mocha , which was later renamed to LiveScript , and finally to JavaScript. The change of name from LiveScript to JavaScript roughly coincided with Netscape adding support for Java technology in its Netscape Navigator web browser. JavaScript was first introduced and deployed in the Netscape browser version 2.0B3 in December 1995. The naming has caused confusion, giving the impression that the language is a spin-off of Java, and it has been characterized by many as a marketing ploy by Netscape to give JavaScript the cachet of what was then the hot new web-programming language.

JavaScript, despite the name, is essentially unrelated to the Java programming language even though the two do have superficial similarities. Both languages use syntaxes influenced by that of C syntax, and JavaScript copies many Java names and naming conventions. The language's name is the result of a co-marketing deal between Netscape and Sun, in exchange for Netscape bundling Sun's Java runtime with their then-dominant browser. The key design principles within JavaScript are inherited from the Self and Scheme programming languages.

"JavaScript" is a trademark of Sun Microsystems. It was used under license for technology invented and implemented by Netscape Communications and current entities such as the Mozilla Foundation.

Due to the widespread success of JavaScript as a client-side scripting language for web pages, Microsoft developed a compatible dialect of the language, naming it JScript to avoid trademark issues. JScript added new date methods to fix the non-Y2K-friendly methods in JavaScript, which were based on java.util.Date. JScript was included in Internet Explorer 3.0, released in August 1996. The dialects are perceived to be so similar that the terms "JavaScript" and "JScript" are often used interchangeably. Microsoft, however, notes dozens of ways in which JScript is not ECMA-compliant.

Netscape submitted JavaScript to Ecma International for standardization resulting in the standardized version named ECMAScript.

JavaScript has become one of the most popular programming languages on the web. Initially, however, many professional programmers denigrated the language because its target audience was web authors and other such "amateurs", among other reasons. The advent of AJAX returned JavaScript to the spotlight and brought more professional programming attention. The result was a proliferation of comprehensive frameworks and libraries, improved JavaScript programming practices, and increased usage of JavaScript outside of the browser, as seen by the proliferation of server-side JavaScript platforms.

Features

The following features are common to all conforming ECMAScript implementations, unless explicitly specified otherwise.

Imperative and structured

JavaScript supports all the structured programming syntax in C (e.g., if statements, while loops, switch statements, etc.). One partial exception is scoping: C-style block-level scoping is not supported (instead, JavaScript has function-level scoping). JavaScript 1.7, however, supports block-level scoping with the let keyword. Like C, JavaScript makes a distinction between expressions and statements. One syntactic difference from C is automatic semicolon insertion, in which the semicolons that terminate statements can be omitted.

Dynamic

Functional

Prototype-based

Miscellaneous

Vendor-specific extensions

JavaScript is officially managed by Mozilla, and new language features are added periodically. However, only some non-Mozilla JavaScript engines support these new features:

  • conditional catch clauses
  • property getter and setter functions
  • iterator protocol adopted from Python
  • shallow generators/coroutines also adopted from Python
  • array comprehensions and generator expressions also adopted from Python
  • proper block scope via new let keyword
  • array and object destructuring (limited form of pattern matching)
  • concise function expressions ( function(args) expr )
  • E4X

Syntax and semantics

Main article: JavaScript syntax

As of 2009, the latest version of the language is JavaScript 1.8.1. It is a superset of ECMAScript (ECMA-262) Edition 3. Extensions to the language, including partial E4X (ECMA-357) support and experimental features considered for inclusion into future ECMAScript editions, are documented here.

Sample code showcasing various JavaScript features:

                        
                          /* Finds the lowest common multiple of two numbers */
                        
                        
                          function
                        
                        LCMCalculator
                        
                          (
                        
                        x
                        
                          ,
                        
                        y
                        
                          )
                        
                        
                          {
                        
                        
                          // constructor function
                        
                        
                          function
                        
                        checkInt
                        
                          (
                        
                        x
                        
                          )
                        
                        
                          {
                        
                        
                          // inner function
                        
                        
                          if
                        
                        
                          (
                        
                        x
                        
                          %
                        
                        1
                        
                          !=
                        
                        0
                        
                          )
                        
                        
                          throw
                        
                        
                          new
                        
                        TypeError
                        
                          (
                        
                        x
                        
                          +
                        
                        
                          " is not an integer"
                        
                        
                          )
                        
                        
                          ;
                        
                        
                          // exception throwing
                        
                        
                          return
                        
                        x
                        
                          ;
                        
                        
                          }
                        
                        
                          //semicolons are optional (but beware since this may cause consecutive lines to be
                        
                        
                          //erroneously treated as a single statement)
                        
                        
                          this
                        
                        .
                        
                          a
                        
                        
                          =
                        
                        checkInt
                        
                          (
                        
                        x
                        
                          )
                        
                        
                          this
                        
                        .
                        
                          b
                        
                        
                          =
                        
                        checkInt
                        
                          (
                        
                        y
                        
                          )
                        
                        
                          }
                        
                        
                          // The prototype of object instances created by a constructor is
                        
                        
                          // that constructor's "prototype" property.
                        
                        LCMCalculator.
                        
                          prototype
                        
                        
                          =
                        
                        
                          {
                        
                        
                          // object literal
                        
                        gcd
                        
                          :
                        
                        
                          function
                        
                        
                          (
                        
                        
                          )
                        
                        
                          {
                        
                        
                          // method that calculates the greatest common denominator
                        
                        
                          // Euclidean algorithm:
                        
                        
                          var
                        
                        a
                        
                          =
                        
                        Math.
                        
                          abs
                        
                        
                          (
                        
                        
                          this
                        
                        .
                        
                          a
                        
                        
                          )
                        
                        
                          ,
                        
                        b
                        
                          =
                        
                        Math.
                        
                          abs
                        
                        
                          (
                        
                        
                          this
                        
                        .
                        
                          b
                        
                        
                          )
                        
                        
                          ;
                        
                        
                          if
                        
                        
                          (
                        
                        a
                        
                          <
                        
                        b
                        
                          )
                        
                        
                          {
                        
                        
                          var
                        
                        t
                        
                          =
                        
                        b
                        
                          ;
                        
                        b
                        
                          =
                        
                        a
                        
                          ;
                        
                        a
                        
                          =
                        
                        t
                        
                          ;
                        
                        
                          // swap variables
                        
                        
                          }
                        
                        
                          while
                        
                        
                          (
                        
                        b
                        
                          !==
                        
                        0
                        
                          )
                        
                        
                          {
                        
                        t
                        
                          =
                        
                        b
                        
                          ;
                        
                        
                          // |t| already declared above (though we could redeclare if we wish)
                        
                        b
                        
                          =
                        
                        a
                        
                          %
                        
                        b
                        
                          ;
                        
                        a
                        
                          =
                        
                        t
                        
                          ;
                        
                        
                          }
                        
                        
                          // Only need to calculate gcd once, so "redefine" this method.
                        
                        
                          // (Actually not redefinition - it's defined on the instance itself,
                        
                        
                          // so that this.gcd refers to this "redefinition" instead of LCMCalculator.prototype.gcd.)
                        
                        
                          // Also, 'gcd' == "gcd", this == this.gcd
                        
                        
                          this
                        
                        
                        
                        
                          =
                        
                        
                          function
                        
                        
                          (
                        
                        
                          )
                        
                        
                          {
                        
                        
                          return
                        
                        a
                        
                          ;
                        
                        
                          }
                        
                        
                          ;
                        
                        
                          return
                        
                        a
                        
                          ;
                        
                        
                          }
                        
                        
                          ,
                        
                        
                          "lcm"
                        
                        
                          /* can use strings here */
                        
                        
                          :
                        
                        
                          function
                        
                        
                          (
                        
                        
                          )
                        
                        
                          {
                        
                        
                          // Variable names don't collide with object properties, e.g. |lcm| is not |this.lcm|.
                        
                        
                          // not using |this.a * this.b| to avoid FP precision issues
                        
                        
                          var
                        
                        lcm
                        
                          =
                        
                        
                          this
                        
                        .
                        
                          a
                        
                        
                          /
                        
                        
                          this
                        
                        .
                        
                          gcd
                        
                        
                          (
                        
                        
                          )
                        
                        
                          *
                        
                        
                          this
                        
                        .
                        
                          b
                        
                        
                          ;
                        
                        
                          // Only need to calculate lcm once, so "redefine" this method.
                        
                        
                          this
                        
                        .
                        
                          lcm
                        
                        
                          =
                        
                        
                          function
                        
                        
                          (
                        
                        
                          )
                        
                        
                          {
                        
                        
                          return
                        
                        lcm
                        
                          ;
                        
                        
                          }
                        
                        
                          ;
                        
                        
                          return
                        
                        lcm
                        
                          ;
                        
                        
                          }
                        
                        
                          ,
                        
                        toString
                        
                          :
                        
                        
                          function
                        
                        
                          (
                        
                        
                          )
                        
                        
                          {
                        
                        
                          return
                        
                        
                          "LCMCalculator: a = "
                        
                        
                          +
                        
                        
                          this
                        
                        .
                        
                          a
                        
                        
                          +
                        
                        
                          ", b = "
                        
                        
                          +
                        
                        
                          this
                        
                        .
                        
                          b
                        
                        
                          ;
                        
                        
                          }
                        
                        
                          }
                        
                        
                          ;
                        
                        
                        
                        
                          ,
                        
                        
                        
                        
                          ,
                        
                        
                        
                        
                          ,
                        
                        
                        
                        
                          ]
                        
                        .
                        
                          map
                        
                        
                          (
                        
                        
                          function
                        
                        
                          (
                        
                        pair
                        
                          )
                        
                        
                          {
                        
                        
                          // array literal + mapping function
                        
                        
                          return
                        
                        
                          new
                        
                        LCMCalculator
                        
                          (
                        
                        pair
                        
                        
                        
                          ,
                          <

The IF Statement

The Javascript if statement.How to code and use it. ... Introduction In the last tutorial you learned about operators that you can use to manipulate text and ...

...

InformIT: Sams Teach Yourself JavaScript in 24 Hours > The if ...

The if Statement. One of the most important features of a computer language is the capability to test and compare values. This allows your scripts to behave differently based on ...

...

Javascript Tutorial - If

Learn how to create an If Statement in Javascript with Tizag.com's Javascript If Statement lesson.

...

Javascript Tutorial - Else If

Learn how to create a Javascript Else If statement with Tizag.com's Javascript Else If lesson.

...

javascript else if statement condition - adding conditions to ...

We've already learnt that conditions can take one of the possible two paths. That is, when the condition is 'true' the if statement block is executed while the statement block in ...

...

Javascript Statements,If and Else Statement,Javascript Statements ...

Javascript Statements, If and Else Statements, Javascript Statements Overview, Nested If Condition Example, Statements Help in Javascript Programming

...

If statement for javascript

if statement for javascript

...

Learn JavaScript - JavaScript If Statement

JavaScript If Statement tutorial offering new to advanced professionals a great resource to learn more about JavaScript If Statement.

...

JavaScript If Statements

Learn about JavaScript If statements (includes Else and Else If).

...

Javascript If...if else statement with HTML code [Archive] - Dynamic ...

[Archive] Javascript If...if else statement with HTML code JavaScript

...