Skip to content

Search is only available in production builds. Try building and previewing the site to test it out locally.

Introduction to Javascript

What is JavaScript ?

JavaScript is a compiled programming language that is lightweight, cross-platform, single-threaded, and interpretable. It is sometimes referred to as the language used for webpage scripts. Although numerous non-browser contexts also use it, it is most renowned for being used in the building of online pages.

JavaScript is a dynamically typed language (or weakly typed) . Both client-side and server-side development can make use of JavaScript. JavaScript is a language that can be declarative or imperative. In addition to a core set of language components like operators, control structures, and statements, JavaScript includes a standard library of objects like Array, Date, and Math.

Client-Side JavaScript

Client-side JavaScript runs in the web browser and is primarily used for creating interactive and dynamic web pages. It can manipulate the Document Object Model (DOM), handle user events, and communicate with the server asynchronously. Examples of client-side JavaScript usage include:

  • Form Validation: Checking user inputs in a form before submitting it to the server.
  • Dynamic Content Updates: Changing the content of a web page without reloading, using techniques like AJAX (Asynchronous JavaScript and XML).
  • User Interface Enhancements: Creating animations, dropdown menus, sliders, and other interactive elements. Example:
clickme.js
document.getElementById("myButton").addEventListener("click", function() {
alert("Button clicked!");
});

Server-Side JavaScript

Server-side JavaScript runs on the server and is used to build scalable and efficient server applications. With the advent of Node.js, JavaScript can now be executed on the server, allowing developers to use the same language for both front-end and back-end development. Server-side JavaScript is used for:

  • Handling HTTP Requests and Responses: Creating RESTful APIs, serving web pages, handling form submissions.
  • Database Operations: Interacting with databases to create, read, update, and delete data.
  • Authentication and Authorization: Managing user sessions, cookies, and implementing security measures. Example with Node.js:
get_from_database.js
const http = require('http');
fetch('https://quotes.toscrape.com/random')
.then((response) => response.text())
.then((body) => {
console.log(body);
});

Imperative Programming

In its imperative form, JavaScript allows developers to write code that specifies step-by-step instructions on how to achieve a certain task. This involves using loops, conditionals, and explicit statements to manipulate data and control the flow of the program. For example:

sum.js
let sum = 0;
for (let i = 0; i < 10; i++) {
sum += i;
}
console.log(sum);

Declarative Programming

JavaScript also supports declarative programming, where developers write code that describes what should be done, rather than how to do it. This is often seen in higher-order functions, such as those used in functional programming paradigms, and in modern libraries like React. For example:

sum.js
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sum);