What is JavaScript
JavaScript is a programming language initially designed to interact with elements of web pages. In web browsers, JavaScript consists of three main parts:
- ECMAScript provides the core functionality.
- The Document Object Model (DOM) provides interfaces for interacting with elements on web pages
- The Browser Object Model (BOM) provides the browser API for interacting with the web browser.
JavaScript allows you to add interactivity to a web page. Typically, you use JavaScript with HTML and CSS to enhance a web page’s functionality, such as validating forms, creating interactive maps, and displaying animated charts.
When a web page is loaded, i.e., after HTML and CSS have been downloaded, the JavaScript engine in the web browser executes the JavaScript code. The JavaScript code then modifies the HTML and CSS to update the user interface dynamically.
The JavaScript engine is a program that executes JavaScript code. In the beginning, JavaScript engines were implemented as interpreters.
However, modern JavaScript engines are typically implemented as just-in-time compilers that compile JavaScript code to bytecode for improved performance.
Client-side vs. Server-side JavaScript
When JavaScript is used on a web page, it is executed in web browsers. In this case, JavaScript works as a client-side language.
JavaScript can run on both web browsers and servers. A popular JavaScript server-side environment is Node.js. Unlike client-side JavaScript, server-side JavaScript executes on the server that allows you to access databases, file systems, etc.
JavaScript History
In 1995, JavaScript was created by a Netscape developer named Brendan Eich. First, its name was Mocha. And then, its name was changed to LiveScript.
Netscape decided to change LiveScript to JavaScript to leverage Java’s fame, which was popular. The decision was made just before Netscape released its web browser product Netscape Navigator 2. As a result, JavaScript entered version 1.0.
Netscape released JavaScript 1.1 in Netscape Navigator 3. In the meantime, Microsoft introduced a web browser product called the Internet Explorer 3 (IE 3), which competed with Netscape. However, IE came with its own JavaScript implementation called JScript. Microsoft used the name JScript to avoid possible license issues with Netscape.
Hence, two different JavaScript versions were in the market:
- JavaScript in Netscape Navigator
- JScript in Internet Explorer.
JavaScript had no standards that governed its syntax and features. And the community decided that it was time to standardize the language.
In 1997, JavaScript 1.1 was submitted to the European Computer Manufacturers Association (ECMA) as a proposal. Technical Committee #39 (TC39) was assigned to standardize the language to make it a general-purpose, cross-platform, and vendor-neutral scripting language.
TC39 came up with ECMA-262, a standard for defining a new scripting language named ECMAScript (often pronounced Ek-ma-script).
After that, the International Organization for Standardization and International Electrotechnical Commissions (ISO/IEC) adopted ECMAScript (ISO/IEC-16262).
JavaScript overview
To define a variable in JavaScript, you use var
keyword. For example:
var x = 10;
var y = 20;
Code language: JavaScript (javascript)
ES6 added a new way to declare a variable with the let
keyword:
let x = 10;
let y = 20;
Code language: JavaScript (javascript)
There are differences between var
and let
. And it’s a good practice to use the let
keyword to declare variables.
To declare a function, you use the function
keyword. The following example defines a function that calculates the sum of two arguments:
function add( a, b ) {
return a + b;
}
Code language: JavaScript (javascript)
To call the add()
function, you use the following syntax:
let result = add(x, y);
Code language: JavaScript (javascript)
To log the result into the console window of the web browser, you use the console.log()
:
console.log(result);
Code language: JavaScript (javascript)
Now, you should see 30
in the console window.
JavaScript provides you with condition statements such as if-else
and switch
statements. For example:
let a = 20,
b = 30;function divide(a, b) {
if(b == 0) {
throw 'Division by zero';
}
return a / b;
}
Code language: JavaScript (javascript)
In the divide()
function, we check whether the de-numerator (b) is zero. If yes, we throw an exception. Otherwise, we return the result of a / b.
To declare an array, you use the following syntax:
let items = [];
Code language: JavaScript (javascript)
To declare an array with some initial elements, you specify the elements in the square brackets:
let items = [1, 2, 3];
Code language: JavaScript (javascript)
You can access the number of elements in the items
array through its length
property:
console.log(items.length); // 3
Code language: JavaScript (javascript)
To iterate over the elements of the items
array, you use the for
loop statement as follows:
for(let i = 0; i < items.length; i++) {
console.log(items[i]);
}
Code language: JavaScript (javascript)
Or use the for...of
loop in ES6:
for(let item of items) {
console.log(item);
}
Code language: JavaScript (javascript)
JavaScript is an evolving language. It has many other features that you’ll learn in the following tutorials.
In this tutorial, you learned what JavaScript is and the overview of the JavaScript language.