What Is JavaScript?

What Is JavaScript?

JavaScript

JavaScript, often abbreviated as JS, is a high-level, interpreted programming language. It is a language which is also characterized as dynamic, weakly typed, prototype-based and multi-paradigm.

Alongside HTML and CSS, JavaScript is one of the three core technologies of the World Wide Web. JavaScript enables interactive web pages and this is an essential part of web applications. The vast majority of websites use it, and all major web browsers have a dedicated JavaScript engine to execute it.

As a multi-paradigm language, JavaScript supports event-driven, functional, and imperative (including object-oriented and prototype-based) programming styles. It has an API for working with text, arrays, dates, regular expressions, and basic manipulation of the DOM, but the language itself does not include any I/O, such as networking, storage, or graphics facilities, relying for these upon the host environment in which it is embedded.

Initially only implemented client-side in web browsers, JavaScript engines are now embedded in many other types of host software, including server-side in web servers and databases, and in non-web programs such as word processors and PDF software, and in runtime environments that make JavaScript available for writing mobile and desktop applications, including desktop widgets.

Although there are strong outward similarities between JavaScript and Java, including language name, syntax, and respective standard libraries, the two languages are distinct and differ greatly in design; JavaScript was influenced by programming languages such as Self and Scheme.

 

Paradigm Multi-paradigm: object-oriented (prototype-based), imperative, functional, event-driven
Designed by Brendan Eich
Developer Netscape Communications Corporation, Mozilla Foundation, Ecma International
First appeared December 4, 1995; 23 years ago
Stable release
ECMAScript 2018 / June 2018; 6 months ago
Preview release
ECMAScript 2019
Typing discipline Dynamic, duck
Filename extensions
  • .js
  • .mjs
Website Mozilla
Major implementations
V8, JavaScriptCore, Rhino, SpiderMonkey, Chakra
Influenced by
Lua, Scheme, Perl, Self, Java, C, Python, AWK, HyperTalk
Influenced
ActionScript, AtScript, CoffeeScript, Dart, JScript .NET, LiveScript, Objective-J, Opa, Perl 6, QML, TypeScript

 

History

Beginnings at Netscape

In 1993, the National Center for Supercomputing Applications (NCSA), a unit of the University of Illinois at Urbana-Champaign, released NCSA Mosaic, the first popular graphical Web browser, which played an important part in expanding the growth of the nascent World Wide Web beyond the NeXTSTEP niche where the WorldWideWeb had formed three years earlier. In 1994, a company called Mosaic Communications was founded in Mountain View, California and employed many of the original NCSA Mosaic authors to create Mosaic Netscape. However, it intentionally shared no code with NCSA Mosaic. The internal codename for the company’s browser was Mozilla, which stood for “Mosaic killer”, as the company’s goal was to displace NCSA Mosaic as the world’s number one web browser. The first version of the Web browser, Mosaic Netscape 0.9, was released in late 1994. Within four months it had already taken three-quarters of the browser market and became the main web browser for the 1990s. To avoid trademark ownership problems with the NCSA, the browser was subsequently renamed Netscape Navigator in the same year, and the company took the name Netscape Communications. Netscape Communications realized that the Web needed to become more dynamic. Marc Andreessen, the founder of the company believed that HTML needed a “glue language” that was easy to use by Web designers and part-time programmers to assemble components such as images and plugins, where the code could be written directly in the Web page markup.

In 1995, Netscape Communications recruited Brendan Eich with the goal of embedding the Scheme programming language into its Netscape Navigator. Before he could get started, Netscape Communications collaborated with Sun Microsystems to include in Netscape Navigator Sun’s more static programming language Java, in order to compete with Microsoft for user adoption of Web technologies and platforms. Netscape Communications then decided that the scripting language they wanted to create would complement Java and should have a similar syntax, which excluded adopting other languages such as Perl, Python, TCL, or Scheme. To defend the idea of JavaScript against competing proposals, the company needed a prototype. Eich wrote one in 10 days, in May 1995.

Although it was developed under the name Mocha, the language was officially called LiveScript when it first shipped in beta releases of Netscape Navigator 2.0 in September 1995, but it was renamed JavaScript when it was deployed in the Netscape Navigator 2.0 beta 3 in December. The final choice of name caused confusion, giving the impression that the language was a spin-off of the Java programming language, and the choice has been characterized as a marketing ploy by Netscape to give JavaScript the cachet of what was then the hot new Web programming language.

There is a common misconception that JavaScript was influenced by an earlier Web page scripting language developed by Nombas named Cmm (not to be confused with the later C– created in 1997). Brendan Eich, however, had never heard of Cmm before he created LiveScript. Nombas did pitch their embedded Web page scripting to Netscape, though Web page scripting was not a new concept, as shown by the ViolaWWW Web browser. Nombas later switched to offering JavaScript instead of Cmm in their ScriptEase product and was part of the TC39 group that standardized ECMAScript.

Server-side JavaScript

In December 1995, soon after releasing JavaScript for browsers, Netscape introduced an implementation of the language for server-side scripting with Netscape Enterprise Server.

Since 1996, the IIS web-server has supported Microsoft’s implementation of server-side Javascript — JScript—in ASP and .NET pages.

Since the mid-2000s, additional server-side JavaScript implementations have been introduced, such as Node.js in 2009.

Adoption by Microsoft

Microsoft script technologies including VBScript and JScript were released in 1996. JScript, a reverse-engineered implementation of Netscape’s JavaScript, was part of Internet Explorer 3. JScript was also available for server-side scripting in Internet Information Server. Internet Explorer 3 also included Microsoft’s first support for CSS and various extensions to HTML, but in each case the implementation was noticeably different from that found in Netscape Navigator at the time. These differences made it difficult for designers and programmers to make a single website work well in both browsers, leading to the use of “best viewed in Netscape” and “best viewed in Internet Explorer” logos that characterized these early years of the browser wars. JavaScript began to acquire a reputation for being one of the roadblocks to a cross-platform and standards-driven Web. Some developers took on the difficult task of trying to make their sites work in both major browsers, but many could not afford the time. With the release of Internet Explorer 4, Microsoft introduced the concept of Dynamic HTML, but the differences in language implementations and the different and proprietary Document Object Models remained and were obstacles to widespread take-up of JavaScript on the Web.

 

Syntax

Simple examples

Variables in JavaScript can be defined using either the var, let or const keywords.

var x; // declares the variable x and assigns to it the special value "undefined" (not to be confused with an undefined value)
var y = 2; // declares the variable y and assigns to it the value 2
var z = "Hello, World!"; // declares the variable z and assigns to it a string containing "Hello, World!"

Note the comments in the example above, all of which were preceded with two forward slashes.

There is no built-in I/O functionality in JavaScript; the run-time environment provides that. The ECMAScript specification in edition 5.1 mentions:

indeed, there are no provisions in this specification for input of external data or output of computed results.

However, most runtime environments have a console object that can be used to print output. Here is a minimalist Hello World program in JavaScript:

console.log("Hello World!");

A simple recursive function:
function factorial(n) {
    if (n === 0) {
        return 1;  // 0! = 1
    }
    return n * factorial(n - 1);
}

factorial(3); // returns 6

An anonymous function (or lambda):

function counter() {
    var count = 0;
    return function() {
        return ++count;
    };
}

var closure = counter();
closure(); // returns 1
closure(); // returns 2
closure(); // returns 3

This example shows that, in JavaScript, function closures capture their non-local variables by reference.

In JavaScript, objects are created in the same way as functions, this is known as a function object.

Object example:

function Ball(r) {
    this.radius = r; //the radius variable is local to the ball object
    this.area = pi*r**2;
    this.show = function(){ //objects can contain functions
        drawCircle(r); //references a circle drawing function
    }
}
myBall = new Ball(5); //creates a new instance of the ball object with radius 5
myBall.show(); //this instance of the ball object has the show function performed on it

Variadic function demonstration (arguments is a special variable):

function sum() {
    var x = 0;
    for (var i = 0; i < arguments.length; ++i) {
        x += arguments[i];
    }
    return x;
}
sum(1, 2); // returns 3
sum(1, 2, 3); // returns 6

Immediately-invoked function expressions are often used to create modules, as before ECMAScript 2015 there was no built-in construct in the language. Modules allow gathering properties and methods in a namespace and making some of them private:

var counter = (function () {
    var i = 0; // private property

    return {   // public methods
        get: function () {
            alert(i);
        },
        set: function (value) {
            i = value;
        },
        increment: function () {
            alert(++i);
        }
    };
})(); // module

counter.get();       // shows 0
counter.set(6);
counter.increment(); // shows 7
counter.increment(); // shows 8

More advanced example

This sample code displays various JavaScript features.

/* Finds the lowest common multiple (LCM) of two numbers */
function LCMCalculator(x, y) { // constructor function
  var checkInt = function(x) { // inner function
    if (x % 1 !== 0) {
      throw new TypeError(x + "is not an integer"); // var a =  mouseX
    };

    return x
  }
  this.a = checkInt(x)
    //   semicolons   ^^^^  are optional, a newline is enough
  this.b = checkInt(y);

}
// The prototype of object instances created by a constructor is
// that constructor's "prototype" property.
LCMCalculator.prototype = { // object literal
  constructor: LCMCalculator, // when reassigning a prototype, set the constructor property appropriately
  gcd: function() { // method that calculates the greatest common divisor
    // Euclidean algorithm:
    var a = Math.abs(this.a),
      b = Math.abs(this.b),
      t;
    if (a < b) {
      // swap variables
      // t = b; b = a; a = t;
      [a, b] = [b, a]; // swap using destructuring assignment (ES6)
    }
    while (b !== 0) {
      t = b;
      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.
    // Note that this leads to a wrong result if the LCMCalculator object members "a" and/or "b" are altered afterwards.)
    // Also, 'gcd' === "gcd", this['gcd'] === this.gcd
    this['gcd'] = function() {
      return a;
    };
    return a;
  },
  // Object property names can be specified by strings delimited by double (") or single (') quotes.
  lcm: 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;
  }
};

// Define generic output function; this implementation only works for Web browsers
function output(x) {
  document.body.appendChild(document.createTextNode(x));
  document.body.appendChild(document.createElement('br'));
}

// Note: Array's map() and forEach() are defined in JavaScript 1.6.
// They are used here to demonstrate JavaScript's inherent functional nature.
[
  [25, 55],
  [21, 56],
  [22, 58],
  [28, 56]
].map(function(pair) { // array literal + mapping function
    return new LCMCalculator(pair[0], pair[1]);
  }).sort((a, b) => a.lcm() - b.lcm()) // sort with this comparative function; => is a shorthand form of a function, called "arrow function"
  .forEach(printResult);

function printResult(obj) {
  output(obj + ", gcd = " + obj.gcd() + ", lcm = " + obj.lcm());
}
The following output should be displayed in the browser window.

LCMCalculator: a = 28, b = 56, gcd = 28, lcm = 56
LCMCalculator: a = 21, b = 56, gcd = 7, lcm = 168
LCMCalculator: a = 25, b = 55, gcd = 5, lcm = 275
LCMCalculator: a = 22, b = 58, gcd = 2, lcm = 638

About author

2,995 Comments

  1. ปั้มไลค์ October 26, 2019 at 12:15 am

    Like!! I blog quite often and I genuinely thank you for your information. The article has truly peaked my interest.

  2. Nebkvoles November 13, 2019 at 4:53 pm

    I was genuinely itching to tails of some wager some monied on some sports matches that are happening above-board now. I wanted to say you guys identify that I did understand what I weigh to be the a-one plat in the USA.
    If you destitution to bring back in on the spirit, verify it out of the closet: try it out

  3. NathanKam December 6, 2019 at 4:54 pm

    Amazing many of helpful knowledge. augmentin antibiotico
    Can You Take Cymbalta And Tramadol

  4. Phillipjek December 8, 2019 at 9:32 am

    You actually mentioned that adequately. cialis 20 mg

  5. Phillipjek December 8, 2019 at 12:30 pm

    You actually mentioned it exceptionally well! provigil

  6. ErnestUnona December 8, 2019 at 4:41 pm

    You stated that fantastically! canadadrugs

  7. ErnestUnona December 8, 2019 at 9:52 pm

    You actually expressed this very well! modafinil

  8. Phillipjek December 8, 2019 at 10:25 pm

    You suggested that really well. modafinil

  9. ErnestUnona December 9, 2019 at 7:25 am

    Helpful postings. Regards! canada rx

  10. RichardBen December 9, 2019 at 12:10 pm

    Really plenty of very good facts. buy modafinil

  11. Phillipjek December 9, 2019 at 2:17 pm

    Incredible many of valuable facts. canada viagra

  12. LouisOnesy December 9, 2019 at 3:11 pm

    With thanks! Fantastic information! cialis 20mg

  13. Phillipjek December 9, 2019 at 5:19 pm

    Thanks a lot. I like it. deltasone

  14. Phillipjek December 9, 2019 at 5:28 pm

    Kudos, Valuable information. zestril

  15. LarryNup December 9, 2019 at 5:37 pm

    Great postings. With thanks! zestril

  16. Phillipjek December 9, 2019 at 6:27 pm

    Nicely put. Thank you. diflucan

  17. LouisOnesy December 9, 2019 at 8:14 pm

    Cheers, I like this! inderal

  18. LarryNup December 9, 2019 at 11:41 pm

    Well spoken of course! ! inderal la

  19. LouisOnesy December 10, 2019 at 12:18 am

    Amazing tons of good advice. baclofen

  20. Phillipjek December 10, 2019 at 12:54 am

    You suggested this superbly! celebrex

  21. LarryNup December 10, 2019 at 1:13 am

    Many thanks, I enjoy it! indera

  22. LouisOnesy December 10, 2019 at 1:39 am

    Thanks, I appreciate this! alopurinol

  23. LarryNup December 10, 2019 at 2:43 am

    You revealed that terrifically! cialis pills

  24. LouisOnesy December 10, 2019 at 3:09 am

    You revealed that perfectly. canada drug

  25. LarryNup December 10, 2019 at 5:46 am

    Seriously a lot of amazing data. motilium

  26. ErnestUnona December 10, 2019 at 6:00 am

    Thanks a lot. I value this. domperidone

  27. Phillipjek December 10, 2019 at 6:27 am

    Amazing facts. Many thanks! diflucan

  28. ErnestUnona December 10, 2019 at 6:29 am

    Very good stuff, With thanks. promethazin

  29. LouisOnesy December 10, 2019 at 8:24 am

    Awesome stuff. Thank you. zestoretic

  30. RichardBen December 10, 2019 at 9:02 am

    Truly lots of valuable knowledge! viagra canada

  31. ErnestUnona December 10, 2019 at 9:16 am

    Great write ups. Kudos. retin-a

  32. ErnestUnona December 10, 2019 at 9:36 am

    Fantastic facts, Cheers! finasteride

  33. Phillipjek December 10, 2019 at 9:36 am

    Fantastic write ups. Many thanks! provigil

  34. LarryNup December 10, 2019 at 9:54 am

    Thank you, Numerous content!
    provigil

  35. LarryNup December 10, 2019 at 12:30 pm

    Thank you! Plenty of knowledge!
    advair

  36. Phillipjek December 10, 2019 at 2:11 pm

    You mentioned this really well. canadian rx

  37. LarryNup December 10, 2019 at 2:26 pm

    You mentioned this wonderfully! motilium

  38. LarryNup December 10, 2019 at 3:48 pm

    Thanks a lot! Lots of postings!
    buy cialis

  39. LouisOnesy December 10, 2019 at 4:41 pm

    Nicely put. With thanks. prinivil

  40. LouisOnesy December 10, 2019 at 5:34 pm

    Perfectly spoken truly! ! flomax

  41. ErnestUnona December 10, 2019 at 5:52 pm

    This is nicely expressed! . provigil

  42. LarryNup December 10, 2019 at 6:20 pm

    Wow quite a lot of useful info! cheap cialis

  43. LouisOnesy December 10, 2019 at 7:25 pm

    Wow a lot of useful information! motilium

  44. LouisOnesy December 10, 2019 at 9:55 pm

    Terrific posts. With thanks! motilium

  45. ErnestUnona December 11, 2019 at 12:09 am

    Very good stuff, Cheers. kamagra gel

  46. Phillipjek December 11, 2019 at 12:19 am

    You revealed this adequately. fluconazole

  47. LarryNup December 11, 2019 at 2:06 am

    You definitely made the point! cephalexin

  48. ErnestUnona December 11, 2019 at 2:23 am

    Nicely put. Cheers! zanaflex

  49. Phillipjek December 11, 2019 at 2:28 am

    Effectively voiced genuinely. . trazodon

  50. ErnestUnona December 11, 2019 at 2:51 am

    Many thanks, Valuable stuff. zoloft

  51. LarryNup December 11, 2019 at 3:26 am

    Thank you. A lot of facts.
    furosemid

  52. Phillipjek December 11, 2019 at 5:48 am

    Regards. Plenty of knowledge.
    vardenafil

  53. LarryNup December 11, 2019 at 6:42 am

    Cheers, I appreciate it! augmentin

  54. LarryNup December 11, 2019 at 9:56 am

    You have made your point. zanaflex

  55. LarryNup December 11, 2019 at 10:07 am

    You actually revealed this effectively. indera

  56. LouisOnesy December 11, 2019 at 10:49 am

    You actually said that really well! fluoxetine

  57. ErnestUnona December 11, 2019 at 12:51 pm

    Regards, A good amount of stuff!
    gabapentin

  58. Phillipjek December 11, 2019 at 2:34 pm

    You said it nicely.! tretinoin

  59. ErnestUnona December 11, 2019 at 5:40 pm

    You suggested that wonderfully! augmentin

  60. Phillipjek December 11, 2019 at 6:09 pm

    Nicely put, Thank you! retin

  61. LouisOnesy December 11, 2019 at 9:01 pm

    Many thanks. Numerous posts.
    flomax

  62. Phillipjek December 11, 2019 at 9:28 pm

    You actually suggested it really well! baclofen 20 mg

  63. LouisOnesy December 11, 2019 at 9:42 pm

    Incredible loads of excellent facts. robaxin-750

  64. ErnestUnona December 11, 2019 at 10:21 pm

    Thanks. Loads of advice!
    furosemida

  65. ErnestUnona December 11, 2019 at 11:38 pm

    Thank you! Terrific information! tamsulosine

  66. Phillipjek December 11, 2019 at 11:59 pm

    Useful info. Thanks a lot. Flomax Sa

  67. LarryNup December 12, 2019 at 12:21 am

    You have made your point quite effectively.! celexa

  68. LouisOnesy December 12, 2019 at 12:39 am

    Factor certainly taken!! furosemid

  69. ErnestUnona December 12, 2019 at 1:00 am

    Kudos. Fantastic information! promethazin

  70. RichardBen December 12, 2019 at 2:51 am

    Awesome forum posts. Regards! cialis 20 mg

  71. ErnestUnona December 12, 2019 at 3:41 am