ECMAScript

Appeared in:
1995
Influenced by:
Influenced:
Paradigm:
Typing discipline:
File extensions:
.es
Dialects:
Versions and implementations (Collapse all | Expand all):
Programming language

ECMAScript is a scripting language standard, implemented and used as its dialects.

ECMAScript was created as a standardized version of JavaScript, so the latter appeared in 1996, but the first standard for the former was published in 1997, giving the language its name and identity.

The standard for ECMAScript is developed by Ecma International and is given in specification ECMA-262, which had several editions, first one published in June 1997, last one (as of December 2009) — in December 2009.

ECMAScript is implemented in the form of its dialects, most popular of which are JavaScript and ActionScript. JavaScript is the most popular scripting language for client-side part of web-applications, and thus usually integrated in a web-browser, though JavaScript interpreters are often embedded in non-web-oriented tools. ActionScript is the programming language used mostly in for Adobe Flash.

ECMAScript is a relatively new language, so it contains features borrowed from a lot of older ones.

Examples:

Hello, World!:

Example for versions SpiderMonkey (Firefox 3.5)

JavaScript uses different commands to output messages depending on what environment is it used in:

  • print: for interpreters with command-line interface prints the message to standard output stream, but when used in web-browser, calls print dialog instead;
  • document.write: when used in web-browser, prints the message to the current document (web-page);
  • console.log: a command for Firebug plugin which prints the message to debug console of the plugin;
  • alert: when used in web-browser, creates a pop-up information window with the message.
print('Hello, World!');

document.write('Hello, World!');

console.log('Hello, World!');

alert('Hello, World!');

Factorial:

Example for versions SpiderMonkey (Firefox 3.5)

This example uses recursive factorial definition and is meant to be executed from the web-browser. To run the example, copy its source text to a file factorial.js and create an HTML file placed in the same directory and containing the following text:

<head><script type="text/javascript" src="factorial.js"></script></head> <body></body>

When you open the HTML file, JavaScript code runs and writes its output to the page shown. document.clear() command clears everything that is written on the currrent web-page.

function factorial(n)
{   if (n == 0)
        return 1;
    else
        return n * factorial(n-1);
}
var i;
document.clear();
for (i = 0; i <= 16; i++)
    document.write(i + "! = " + factorial(i) + "<br />");

Fibonacci numbers:

Example for versions SpiderMonkey (Firefox 3.5)

This example uses recursive definition of Fibonacci numbers and is meant to be executed from web-browser.

function fibonacci(n)
{   if (n<3)
        return 1;
    else
        return fibonacci(n-1) + fibonacci(n-2);
}
var i;
document.clear();
for (i = 1; i <= 16; i++)
    document.write(fibonacci(i) + ", ");
document.write("...<br />");

Quadratic equation:

Example for versions SpiderMonkey (Firefox 3.5)

The example is meant to be executed from the web-browser. To run the example, copy the code to a file quadratic.js and create an HTML file placed in the same directory and containing the following text:

<head>
<script type="text/javascript" src="1.js"></script>
</head>
<body>
<form name="quadratic">
    <input type="number" required="required" name="A">
    <input type="number" required="required" name="B">
    <input type="number" required="required" name="C">
    <input type="button" value="Solve" onClick="solve()">
</form>
<p id="output">
</p>
</body>

This will create a web-page with three input fields and a button. The equation will be solved once the button is pressed, and the roots will be printed beneath the inputs.

JavaScript doesn’t provide complex numbers, so the calculations are done in real numbers with separate check for discriminant sign. This implementation allows to solve quadratic equations not only with integer coefficients but also with floating-point ones.

function print(real, imag) 
{   if (Math.abs(imag)<1E-6)
        return real;
    else
        return '('+real+','+imag+')';
}

function solve() 
{   A = document.quadratic.A.value;
    if (Math.abs(A)<1E-3)
    {   document.getElementById('output').innerHTML = 'Not a quadratic equation';
        return;
    }
    B = document.quadratic.B.value;
    C = document.quadratic.C.value;
    A = 2*A;
    D = B*B-2*A*C;
    if (Math.abs(D)<1E-3)
    {   document.getElementById('output').innerHTML = 'x = '+(-B/A);
        return;
    }
    if (D>0)
        document.getElementById('output').innerHTML = 'x1 = '+print((-B+Math.sqrt(D))/A, 0)+'<br />x2 = '+print((-B-Math.sqrt(D))/A, 0);
    else
        document.getElementById('output').innerHTML = 'x1 = '+print(-B/A,Math.sqrt(-D)/A)+'<br />x2 = '+print(-B/A,-Math.sqrt(-D)/A);
}