Introduction to JavaScript | Web Development Scripting

JavaScript is a high-level, interpreted programming language primarily used for creating interactive and dynamic content on web pages. It allows developers to enhance the user experience by manipulating the DOM (Document Object Model) of a webpage, handling user input, and interacting with external APIs. JavaScript is an essential language for building modern websites and web applications and is considered one of the three core technologies of web development, alongside HTML and CSS.

Key Features of JavaScript:

Simple JavaScript Program Example:

This example demonstrates a simple JavaScript program that outputs a greeting message:

Example


console.log("Hello, World!");

Output

Hello, World!

JavaScript Variables and Data Types:

In JavaScript, variables are used to store data. JavaScript is a loosely typed language, meaning that variables can hold values of any type and the type can change at runtime.

Example of JavaScript Variables:

This example demonstrates how to declare variables using var, let, and const:

Example


var name = "John";
let age = 30;
const country = "USA";
console.log(name);  // John
console.log(age);   // 30
console.log(country); // USA

Output

John 30 USA

Functions in JavaScript:

Functions in JavaScript are blocks of reusable code that are executed when they are called. Functions are fundamental building blocks in JavaScript, allowing you to organize and manage code efficiently.

Example of a JavaScript Function:

This example shows a function that takes two parameters and returns their sum:

Example


function addNumbers(a, b) {
    return a + b;
                }
console.log(addNumbers(5, 3));  // 8

Output

8

Objects and Arrays in JavaScript:

JavaScript has two primary data structures used to store multiple values: objects and arrays.

Example of Objects and Arrays:

This example demonstrates how to create an object and an array in JavaScript:

Example


let person = {
    name: "John",
    age: 30,
    country: "USA"
};

let numbers = [1, 2, 3, 4, 5];

console.log(person.name);  // John
console.log(numbers[2]);   // 3
                           

Output

John 3

Getting Started with JavaScript:

To start coding in JavaScript, you need a text editor (such as Visual Studio Code, Sublime Text, or Atom) and a browser (such as Google Chrome, Firefox, or Safari). You can write your JavaScript code inside an HTML file using the <script> tag, or in a separate JavaScript file with the .js extension.