What is JavaScript?

JavaScript is a programming language that allows you to upgrade the features of a web page to make it dynamic. According to W3Techs, JavaScript powers around 97.6% of all websites on the internet; this is why it is the third building block of modern web development– after HTML and CSS. 

JavaScript is a client-side language– meaning that the code is processed on the browser rather than on the server. The fact that JavaScript runs on the browser makes its operations run faster because there is no need to get a response from the server before carrying out instructions. Also, JavaScript can validate requests before sending them to the server; therefore, if used correctly, JavaScript can make errors occur less frequently.

History Of JavaScript

The Netscape Days

Before the development of browsers such as ChromeFirefox, and Opera, the most popular browser was Netscape– developed by Marc Andreessen. In 1995 web browsing was nothing like it is now; most websites were static HTML pages; websites with any functionality had to do it across the server.

The team at Netscape envisioned a future where websites were more dynamic in operation; thus, the idea of a scripting language that runs on the browser was born. They wanted this language to be simple and easy for even non-programmers to use in adding functionality to their websites. Brendan Eich was contracted for this job and created the LiveScript programming language in ten days.

The name was changed to JavaScript upon its release to piggyback off the growing popularity of the Java programming language; despite the lack of relation between both languages. Subsequent Netscape versions included an interpreter for JavaScript.

In that same year, Microsoft released its flagship web browser– Internet Explorer, which became a direct competitor to Netscape. Microsoft reverse-engineered the JavaScript interpreter and in 1996 released the “new language” known as JScript

Each iteration of JavaScript and JScript brought more differences between both languages. Soon it was too hard for developers to keep up with making websites compatible on both Netscape and Internet Explorer, and they had to “pick a side” by choosing between JavaScript and JScript compatibility. 

Post Netscape JavaScript

Eventually, Internet Explorer won the browser war of the late 90s, leading to Netscape’s demise and the stalling of JavaScript updates. When Internet Explorer finally had a monopoly of the browser market, they did not make improvements to JScript; as the web evolved, the browser stalled.

In 2004, a free software community of Netscape members called Mozilla developed the Firefox browser. Firefox was well-received and eventually wrested the browser share top-spot from Internet Explorer because of its innovative features. The rise in Firefox’s popularity led to a revival of JavaScript’s popularity.

In the coming years, JavaScript grew in popularity, and developers built frameworks around the language. The launch of Google’s Chrome browser in 2008 was another critical moment in JavaScript history.

The browser used the V8 JavaScript engine, which used just-in-time compilation (JIT) to make JavaScript run faster on the browser; soon, other browsers had to incorporate JIT into their engines.

The growing need for language standardization was accomplished in July 2008 when all the key players in the language met for a conference in Oslo, Norway. This conference led to the unification of all relevant work around the language; this unification led to the release of ECMAScript 5 standard of JavaScript in December 2009 as the official JavaScript language.

Uses Of Javascript

There is almost no website you visit on the internet that doesn’t have a trace of JavaScript on it. Many websites become a shell of themselves without JavaScript enabled, while others such as YouTube, Netflix, and Twitter won’t even work without JavaScript.

Twitter won’t load if JavaScript is disabled on your browser

Let’s look at some uses of the JavaScript language:

1. Website Interactivity

By using JavaScript, you can make your simple website interactive; in other words, when a user takes action on a website, the website can respond. JavaScript makes a website interactive by listening to an HTML element for a specific event. 

Website interactivity allows a webpage to update automatically without reloading, allowing developers to create dynamic websites. A byproduct of interactivity is improved browsing speed since the script runs on the browser.

Also, this can improve overall server performance as requests will only be sent to the server that JavaScript cannot handle, rather than bombard the server with basic operations.

2. User Experience And Interface Improvements

Features such as pop-ups, tooltips, and dialog boxes can significantly improve any website’s user experience and interface. JavaScript makes it possible for developers to implement these features on their websites.

3. Client-side Validation

Regardless of their size, forms are a common part of many websites. Form inputs that require custom user inputs, like the <textarea> and <input> elements are susceptible to invalid user inputs. Sending wrong inputs to the server might lead to the server returning errors.

Many server-side programs have input validation techniques; however, it’s better to be able to validate on the browser. Client-side validation reduces the number of unnecessary requests sent to the server, freeing up the server for other valuable processes. 

Furthermore, many server-side programs don’t handle input errors elegantly, or they send back status codes, which users who don’t know how to use Developer Tools might not find. Doing it instantly and on the frontend is the best way to handle these validations, while you might use the backend validation as a failsafe.

4. Animation

JavaScript can also be used for the animation of HTML elements on the Document Object Module (DOM). This can be done by using JavaScript to program gradual changes in an element’s style. In the example below, we use JavaScript to create a simple animation of a small box moving within a container.  

<!DOCTYPE html>
<html>
<style>
#container {
  width: 400px;
  height: 400px;
  position: relative;
  background: yellow;
}
#animate {
  width: 50px;
  height: 50px;
  position: absolute;
  background-color: red;
}
</style>
<body>

<p><button onclick="myMove()">Click Me</button></p> 

<div id ="container">
  <div id ="animate"></div>
</div>

<script>
function myMove() {
  let id = null;
  const elem = document.getElementById("animate");   
  let pos = 0;
  clearInterval(id);
  id = setInterval(frame, 5);
  function frame() {
    if (pos == 350) {
      clearInterval(id);
    } else {
      pos++; 
      elem.style.top = pos + "px"; 
      elem.style.left = pos + "px"; 
    }
  }
}
</script>

</body>
</html>

5. Backend Web Development

A website’s backend is the invisible part that consists of a server, a database, and applications. While a website can work perfectly without a backend, it is necessary for instances where the user would need constant access to files and features on a server or database.

You can use JavaScript for backend development using a framework known as Node.js. Node.js allows us to use Javascript to handle data updates from the front end and build scalable network applications. One of Node’s strengths is its asynchronous nature, meaning simultaneous functions can run in the background without blocking one another.

Millions of developers use Node.js, and Node Package Manager npm has the most modules of any package manager globally. The framework is so popular that there are other popular frameworks such as Express.js built on it.

6. Frontend Web Development

JavaScript is no longer used just for adding frontend functionality to a website; nowadays, the entire frontend of websites is built entirely with JavaScript. Frontend development is possible through the use of JavaScript frameworks.

The earliest instance of JavaScript’s involvement in frontend development was via the creation of the library known as jQuery, which made DOM traversal significantly easier. With time, JavaScript frameworks like ReactVue, and Angular have become the building block of many popular websites.

7. Mobile Application

While it might not be a first choice use of JavaScript, mobile app development with JavaScript is also possible through frameworks such as jQuery MobileReact Native and Meteor. Most mobile apps created using JavaScript are lightweight and cross-platform compatible.

8. Game Development

You can use javascript to create light games that users can play on the browser; the only requirement for this is a strong knowledge of DOM manipulation.

9. Desktop Application Development

You can also use JavaScript for the development of desktop applications. As with mobile applications, these apps have the advantage of being cross-platform and would run smoothly on Windows, Mac, and Linux. Desktop application development with JavaScript is possible using frameworks such as ElectronJSNodeGUI, and NW.js.

Basic Understanding of JavaScript and its Syntax

Syntax refers to the rules that define the structure of a programming language; they define things such as punctuations, symbols, and words used in a programming language.

There are rules to human languages, and flaunting them will lead to a breakdown in communication; disobeying a programming language’s syntax will lead to errors compiling or interpreting the code. Below are the rules of the JavaScript programming language:

1. JavaScript Is Case Sensitive

It’s important to know that Javascript is a case-sensitive language; any alteration to the case used in keywords, identifiers, or values alters the reference. For example, the keyword Function is not the same as function.

In JavaScript, there are instances where you want to name an identifier that contains more than one word. The standard means of multi-word naming in Javascript is lower camel case, which means the first alphabet starts with lower case. Every new word begins with an upper case, for example, multiWordVariable.

2. Understanding JavaScript Statements

Most JavaScript programs consist of many statements; statements are instructions given by a developer that the computer is supposed to execute. JavaScript carries out statements in order, from top to bottom. JavaScript statements consist of:

  • Keywords are the predefined words used in JavaScript that identify what action the computer should perform; for example, let keyword tells JavaScript to create a new variable. 
  • Identifiers– are the names given to particular keywords you create; you can give them almost any name you choose. Identifiers are used for particular statements so that you can refer to them later in the program.
  • Operators are symbols used to perform operations in JavaScript; the most popular operator is the assignment operator, a “=” symbol. 
  • Values– Values are the content of any given statement; in most cases, you choose what the values are. 
  • Expressions– are units of code that result in value; because expressions amount to values, you can place them as a substitute for values. For example, you can use the expression 1+1 instead of the numeric value 2.
  • Comments– Comments in any programming language are a way to add text into a program that the computer should not interpret as code. Comments can help the programmer later understand why they did something in the code. There are single and multi-line comments in JavaScript.
  • Semi-colons should be placed at the end of every statement in JavaScript. Not inserting the semi-colon after a statement doesn’t cause any errors in JavaScript, but it is considered best practice.

Sample of JavaScript Expressions:

let variableOne = “cloudDevelop”;
let variableTwo = 2 + 2
// Single line comment

/* Multi
Line
Comment */

3. Rules For JavaScript Identifiers

There are a few simple rules that guide how you can name identifiers in JavaScript; they are:

  • The first character must begin with A-Z, a-z, $, or _.
  • The first character of an identifier cannot be a number like 1.
  • Subsequent characters may be letters, numbers, underscores, or dollar signs.
  • There can be no space between characters.

4. Variables And Functions

Variables are a way to save a value in JavaScript so that you can call the value at any point in the code using the identifier. In ES6, there are two types of variables:

  • const– allows you to create constant variables whose values, in most cases, can’t be modified.
  • let– which allows you to create variables whose values you can modify later.
const myName = “Stephen” // constant variable
let myAge = 26 // variable that can be changed

A function is a block of code designed to perform a specific task. Functions should first be declared, and at any point they are needed, you will invoke them by adding a () in front. The most basic function syntax is as follows:

function addNumbers() {
	1 + 1;
}
// the addNumbers function can be called below:
addNumbers();

5. Data Types

You can divide data types in JavaScript into primitive values and objects. Primitive values are represented directly at the lowest level of the language. Examples of primitive values are:

  • String– JavaScript strings represent textual data; the contents of a string are put in either double or single quotes (“” or “).
  • Numbers are data types used to represent numeric data; numbers should not be in quotes, or they’ll become strings.
  • Booleans are logical entities with only two values– true or false.
  • Undefined– If you declare a variable without assigning it a value, it will automatically be undefined.
  • Null– When you refer to a nonexistent or invalid object or address, JavaScript returns a null value.

JavaScript objects are data types that can be a collection of properties; some object examples are:

  • “Normal” objects– A JavaScript object is a collection of items with a key and value.
  • Arrays allow you to store a collection of items under a single variable name.
  • Dates– The built-in JavaScript utility allows you to represent a single moment in time; the value returned is usually the number of milliseconds since 1 January 1970 UTC.
  • JSON– JSON (JavaScript Object Notation) is a lightweight data-interchange format derived from JavaScript but used by many programming languages.

Read More: What is a Javascript Map?

Leave a Comment