Introduction to JavaScript: Understanding the Essentials #1

Introduction

JavaScript is one of the most widely used programming languages, powering millions of websites and services. It’s versatile, adaptable, and necessary for web development. In this comprehensive introduction, we will look at the fundamentals of JavaScript, its history, and how it differs from other programming languages such as Java. We will also examine how JavaScript is executed and analyze its dynamic and loosely typed nature.

What is JavaScript?

JavaScript is a high-level, interpreted programming language designed largely for web applications. Brendan Eich designed it in 1995 while working for Netscape Communications Corporation. JavaScript provides interactive web pages and is a necessary component of web applications. JavaScript, like HTML and CSS, is one of the foundational technologies of the World Wide Web.

Key Features of JavaScript

  1. Interpreted Language: JavaScript code is executed line by line, making it an interpreted language.
  2. Event-Driven: JavaScript is often used to handle events such as user clicks, form submissions, and page loads.
  3. Lightweight: JavaScript is designed to be easy to embed in web pages and applications.
  4. Prototype-Based: JavaScript uses prototypes rather than classical inheritance.
  5. First-Class Functions: Functions in JavaScript are treated as first-class objects, allowing them to be assigned to variables, passed as arguments, and returned from other functions.

How JavaScript is Executed

JavaScript is a popular computer language used for web development. Understanding how JavaScript is performed is critical for novices to grasp its operation and build more efficient and effective code. Here, we’ll go over the procedure step by step and present examples to help make things apparent.

What Happens When You Run JavaScript?

When you write and run JavaScript, several things happen behind the scenes. Let’s explore this process:

  1. Loading the Script
  2. Parsing the Code
  3. Compilation and Execution
  4. Execution Context
  5. Call Stack
  6. Event Loop and Callback Queue

1. Loading the Script

When a browser loads a web page, it reads the HTML file and identifies the JavaScript files linked to it. This can be done using the <script> tag:

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript Execution</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <script src="script.js"></script>
</body>
</html>

In this example, the browser finds the script.js file and loads it.

2. Parsing the Code

Once the script is loaded, the JavaScript engine parses it. Parsing is the process of reading and transforming code into a format that a computer can understand. This procedure checks for syntax errors and gets the code ready for execution.

JavaScript
// script.js
console.log("Hello, World!");

3. Compilation and Execution

JavaScript is often referred to as an interpreted language, but modern JavaScript engines (like V8 in Google Chrome) use Just-In-Time (JIT) compilation. This means the code is compiled into machine code just before it is executed, making the execution faster.

4. Execution Context

The JavaScript engine creates an execution context, which is an environment where the code is executed. There are two main types of execution contexts:

  1. Global Execution Context: Created when the script starts execution. It handles the global code.
  2. Function Execution Context: Created whenever a function is called. Each function call gets its own execution context.

JavaScript
// Global Execution Context
let greeting = "Hello, World!";

function greet() {
    // Function Execution Context
    console.log(greeting);
}

greet();

5. Call Stack

The call stack is a mechanism for keeping track of function calls. When a function is called, it is added to the call stack. When the function returns, it is removed from the stack. If the stack overflows, it results in a “stack overflow” error.

JavaScript
function first() {
    console.log("First function");
    second();
}

function second() {
    console.log("Second function");
    third();
}

function third() {
    console.log("Third function");
}

first();

// Call Stack:
// 1. first()
// 2. second()
// 3. third()

6. Event Loop and Callback Queue

JavaScript is single-threaded, meaning it can only do one thing at a time. However, it handles asynchronous operations (like fetching data from an API) using the event loop and callback queue.

Event Loop: Continuously checks the call stack and the callback queue.

Callback Queue: Holds callbacks (functions) that are ready to be executed after asynchronous operations complete.

JavaScript
console.log("Start");

setTimeout(() => {
    console.log("Callback");
}, 2000);

console.log("End");

// Output:
// Start
// End
// Callback (after 2 seconds)

In this example, the setTimeout function schedules the console.log("Callback") to be executed after 2 seconds. The main code continues to execute, and the event loop eventually picks up the callback from the queue.

Putting It All Together

JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript Execution Example</title>
</head>
<body>
    <h1>JavaScript Execution Example</h1>
    <script>
        console.log("Script Start");

        function first() {
            console.log("First function start");
            second();
            console.log("First function end");
        }

        function second() {
            console.log("Second function start");
            setTimeout(() => {
                console.log("Callback from second function");
            }, 1000);
            console.log("Second function end");
        }

        first();

        console.log("Script End");
    </script>
</body>
</html>

Output Explanation:

  1. console.log("Script Start"); logs “Script Start”.
  2. The first function is called.
  3. Inside first, “First function start” is logged.
  4. The second function is called.
  5. Inside second, “Second function start” is logged.
  6. The setTimeout schedules a callback.
  7. “Second function end” is logged.
  8. second function returns to first.
  9. “First function end” is logged.
  10. first function returns to the global context.
  11. “Script End” is logged.
  12. After 1 second, the callback from setTimeout logs “Callback from second function”.

Final Output

JavaScript
Script Start
First function start
Second function start
Second function end
First function end
Script End
Callback from second function

Dynamic and Weakly Typed Language

During your study of JavaScript, you may encounter phrases like “weak typing” and “dynamic typing.” It is essential to comprehend these ideas because they specify how JavaScript handles types and variables. We’ll discuss what it means for JavaScript to be a dynamic and weakly typed language in this approachable tutorial, complete with concise examples to help you understand the ideas.

Dynamic Typing

Dynamic typing means that the type of a variable is determined at runtime, rather than at compile time. In other words, you don’t need to declare the type of a variable when you create it. You can assign any type of value to a variable, and JavaScript will handle it dynamically.

Example

JavaScript
let value; // No type is specified

value = 42;      // value is now a number
console.log(value); // Output: 42

value = "Hello"; // value is now a string
console.log(value); // Output: Hello

value = true;    // value is now a boolean
console.log(value); // Output: true

In this example, the variable value starts without any type. It first holds a number, then a string, and finally a boolean. JavaScript dynamically determines the type of value at runtime.

Weak Typing

Weak typing means that JavaScript allows operations between different types without explicit type conversion. This can lead to unexpected results because JavaScript tries to convert values to a common type automatically.

Example:

JavaScript
let result;

result = "5" + 5; // String concatenation
console.log(result); // Output: "55"

result = "5" - 2; // Numeric subtraction
console.log(result); // Output: 3

result = "5" * "2"; // Numeric multiplication
console.log(result); // Output: 10

result = 5 + true; // true is converted to 1
console.log(result); // Output: 6

In this example, you can see how JavaScript performs type coercion. When using the + operator with a string and a number, it performs string concatenation. However, with - and *, it converts the strings to numbers and performs arithmetic operations.

Why JavaScript is Dynamic and Weakly Typed

JavaScript’s dynamic and weak typing features make it a flexible and easy-to-use language, especially for beginners. However, they can also lead to unexpected behavior if you’re not careful.

Benefits:

  1. Flexibility: You can easily change the type of a variable without worrying about type declarations.
  2. Ease of Use: Beginners can quickly write code without understanding complex type systems.

Drawbacks:

  1. Unpredictable Behavior: Automatic type conversion can lead to bugs that are hard to detect.
  2. Reduced Readability: Code can become harder to understand when types are not explicitly defined.

JavaScript vs. Java

Java and JavaScript are two popular programming languages, however despite their similar names, they perform different functions and have distinct characteristics. This information attempts to assist novices in understanding the fundamental distinctions between Java and JavaScript, their applications, and why it is critical to know which one to use for specific jobs.

The Origins

Java:

  1. Developed by Sun Microsystems (now owned by Oracle) in 1995.
  2. Created with the intention of being a versatile, platform-independent programming language.
  3. Initially aimed at interactive television, it later became popular for building server-side applications, Android apps, and enterprise-level systems.

JavaScript:

  1. Created by Brendan Eich at Netscape in 1995.
  2. Originally called Mocha, then LiveScript, before being renamed to JavaScript.
  3. Designed to make web pages interactive and dynamic.
  4. Primarily used for client-side scripting in web development, but now also used on the server-side with Node.js.

Key Differences

Syntax and Execution:

  1. JavaScript: Primarily used for web development and executed in browsers.
  2. Java: A general-purpose programming language used for building various applications and is executed on the Java Virtual Machine (JVM).

Typing:

  1. JavaScript: Dynamically and weakly typed.
  2. Java: Statically typed, meaning variable types must be declared and cannot change.

Inheritance:

  1. JavaScript: Uses prototype-based inheritance.
  2. Java: Uses class-based inheritance.

Concurrency:

  1. JavaScript: Uses a single-threaded event loop model.
  2. Java: Supports multithreading, allowing multiple threads to run concurrently.

Example in JavaScript

JavaScript
function greet(name) {
    console.log(`Hello, ${name}!`);
}

greet('Alice'); // Outputs: Hello, Alice!

Equivalent example in Java

Java
public class Main {
    public static void main(String[] args) {
        greet("Alice");
    }

    public static void greet(String name) {
        System.out.println("Hello, " + name + "!");
    }
}

Brief History of JavaScript

1995: Birth of JavaScript

Brendan Eich created JavaScript in roughly ten days in May 1995, while working at Netscape Communications Corporation. It was first called Mocha, then LiveScript, and eventually JavaScript.

1996-1997: Standardization

In 1996, Netscape submitted JavaScript to ECMA International for standardization. The first edition of ECMAScript (the standardized version of JavaScript) was published in 1997.

1999: ECMAScript 3

ECMAScript 3, released in 1999, introduced several important features, including regular expressions, better string handling, and new control statements.

2009: ECMAScript 5

ECMAScript 5 (ES5), released in 2009, brought significant improvements such as strict mode, JSON support, and new array methods.

2015: ECMAScript 6 (ES6/ES2015)

ES6, released in 2015, was a major update that introduced many new features, including classes, modules, arrow functions, promises, template literals, and destructuring assignments.

Recent Updates

Since ES6, the language has continued to evolve with annual updates, adding new features and improvements. Notable recent updates include:

  1. ES7 (2016): Introduced Array.prototype.includes and the exponentiation operator (**).
  2. ES8 (2017): Added async functions and shared memory.
  3. ES9 (2018): Introduced rest/spread properties and asynchronous iteration.
  4. ES10 (2019): Brought features like Array.prototype.flat, Object.fromEntries, and optional catch binding.
  5. ES11 (2020): Added BigInt, dynamic import(), and nullish coalescing operator (??).

JavaScript has evolved significantly since its introduction in 1995. Its dynamic and weakly typed characteristics, together with its adaptability and broad browser support, make it an indispensable language for current web development. Understanding its execution method, distinctions from Java, and historical evolution lays a solid basis for learning JavaScript. As the language evolves, maintaining current on the latest features and best practices can help you remain proficient and effective in your web development pursuits.

CSSHTMLJavascript

Recent Blogs

Understanding JavaScript Operators: A Comprehensive Guide #3

Introduction to JavaScript Operators In JavaScript, as in other programming languages, operators are special symbols or keywords that execute operations on operands (variables, constants, or values). Simply said, an operator accepts one or more operands, manipulates them in a certain way, and produces a result. For example, in the phrase 3 + 4, the + […]

CSSHTMLJavascript

Web Storage API Essentials: Everything You Need to Know to Get Started – #1

Web Storage API Essentials – In today’s web development market, effective client-side data management is critical for providing seamless user experiences.

CSSHTMLJavascriptWeb Storage

Easy Guide to Excel’s INDIRECT Function #1

How to Use Excel’s INDIRECT Function In the Excel, the INDIRECT function is a versatile tool that can considerably improve your spreadsheet capabilities. Whether you’re a data analyst, financial planner, or casual user, knowing how to use the INDIRECT function will help you optimise workflows and offer dynamic flexibility to your data management. This blog […]

ExcelMicrosoft

12 Essential Design Principles Explained: Your Comprehensive Manual

Design principles are basic ideas and rules that direct the process of creating visually beautiful, useful, and efficient designs in a variety of fields

Design PrinciplesUi Design