Introduction to JavaScript

Introduction to JavaScript

The lingua franca of the web and its features.

ยท

5 min read

The World Wide Web (www) was invented by Tim Berners-Lee while working at CERN (European Organization for Nuclear Research). He also developed the first web server and the first browser. Tim Berners-Lee is known for inventing a document formatting protocol called HTML (HyperText Markup Language) in 1991.

The inventions of Tim Berners-Lee ushered in the creation of other web pages. These web pages were primarily informational and didn't offer interactivity based on a visitor's action or behaviour on the web page.

These web pages are often referred to as Static websites.

History of JavaScript

In 1995, while working for Netscape, Brendan Eich developed a new programming language called LiveScript. Developing this language was intended to make web pages dynamic and interactive.

LiveScript was finally renamed JavaScript during its official release. Since Java was the prevalent language then, this move is widely regarded as a marketing ploy by its founders.

JavaScript has gone from a scripting language for web pages to a core web technology. It can now be used to create mobile apps and program servers through frameworks and libraries like NodeJs, React Native, etc.

JavaScript can be referred to as the Lingua Franca of the web, as most web pages are created with it.

Also, statistics from StackOverflow indicate JavaScript is the most popular programming language.

Features of JavaScript

JavaScript has many features that differentiate it from other programming languages or qualify it as a programming language. These features will be discussed below, which would help anyone appreciate the language JavaScript.

i. Multi-paradigm: The JavaScript language is a multi-paradigm programming language because it supports various programming styles, including object-oriented and functional programming. Developers can also mix the various paradigms in JavaScript code or choose to work with one paradigm. Below are code examples of the various programming paradigms in JavaScript:

const person = {
    name: "John Doe",
    age: 25
}

function isAdult(){
    if(staff.age > 18){
        console.log( "This is an adult" );
    }else{
        console.log( "This is a teenager or a child" );
    }
}

isAdult();
// This is an adult
class User {
  constructor(name, age, married){
      this.name = name;
      this.age = age;
      this.married = married
  }

  isMarried(){
      if(this.married){
          console.log( this.name + " is married");
     }else{
         console.log( this.name + " is single");
     }
  }
}

let firstUser = new User("Will Smith", 30, true);
let secondUser = new User("Kipoge Chimpopo", 45, false);

console.log(firstUser);
// User {name: "Will Smith", age: 30, married: true}

console.log(secondUser);
// User {name: "Kipoge Chimpopo", age: 45, married: false}

firstUser.isMarried()
// Will Smith is married

secondUser.isMarried()
 // Kipoge Chimpopo is single

The first code snippet is the use of the functional programming paradigm. The isAdult function is used to check if the age property of a Person object is above 18 and outputs a result accordingly.

The second code snippet depicts the use of an object-oriented programming paradigm. The User class is created and used as a prototype for creating other user objects, which eventually inherit the methods and properties of the User class. If you wish to understand Object-oriented Programming in JavaScript better, click this link.

ii. Single-Threaded: JavaScript at runtime is single-threaded. This means it maintains a single call stack and one memory heap, which means codes are executed synchronously.

This means that JavaScript codes are executed in order and must finish executing a particular line of code before jumping into the following line. Take a look at the code snippet below:

let name = "Kylian Thomas";

alert(name);
// Kylian Thomas

const age = 25;

console.log(age);
// 25

From the code snippet above, the alert method will alert the name variable (Kylian Thomas) before the fourth line of code runs and logs the age variable (25) to the console.

iii. Interpreted Language: JavaScript is a high-level language, meaning codes are written in a more user-friendly context. High-level languages usually need a compiler or an interpreter to convert their codes to machine-readable ones.

But, JavaScript codes are not converted into machine-readable code before execution, which makes it an Interpreted language. The JavaScript engines located in various browser runs over every line of code, interprets it and runs it.

It is also worth noting that modern browsers use the JIT (Just In Time) Compilation approach. JIT Compilation is a way of executing computer code that involves compilation during the execution of a program at runtime.

iv. Dynamic typing: Unlike in programming languages like Java, variables (data) in JavaScript are declared and assigned without defining their data type. The JavaScript deduces the data type of the variable based on the value assigned to the variable during runtime.

This feature is frowned upon by developers familiar with strongly-typed languages, claiming that such a feature could lead to more errors at runtime.

v. Prototype-based language: This means that objects in JavaScript can be created without defining their classes. Prototype-based programming is a style of Object-oriented programming language.

Every object in JavaScript has a built-in property called its prototype. A prototype is an object used as a template to get a new object's initial properties.

Other features of JavaScript include; case sensitivity, lightweight scripting language, platform independence, closures etc.

Conclusion

The above features of JavaScript discussed and listed above are a partial list of the features of the programming language.

Moreover, these features are essential in understanding the JavaScript language. Beginners interested in learning the language will find these features helpful in understanding the workings of JavaScript.


If you find this article ambiguous or helpful, I would like to know in the comment section. Also, I would appreciate it if you comment on any topic you would love me to write on.

Lastly, follow my blog to enjoy more insightful blog posts on programming concepts and techniques. Gracias ๐Ÿ’—๐Ÿ’—

ย