Basic of JavaScript

Basic of JavaScript

·

4 min read

JavaScript is a versatile and essential programming language that adds interactivity, dynamic behavior, and client-side scripting to web pages. Here's a more detailed overview of the basics of JavaScript:

How Javascript Engine’s Work? – Sciencx

JavaScript (JS) is a lightweight interpreted (or just-in-time compiled) programming language with first-class functions. While it is most well-known as the scripting language for Web pages, many non-browser environments also use it, such as Node.js, Apache CouchDB and Adobe Acrobat. JavaScript is a prototype-based, multi-paradigm, single-threaded, dynamic language, supporting object-oriented, imperative, and declarative (e.g. functional programming) styles.

JavaScript's dynamic capabilities include runtime object construction, variable parameter lists, function variables, dynamic script creation (via eval), object introspection (via for...in and Object utilities), and source-code recovery (JavaScript functions store their source text and can be retrieved through toString()).

This section is dedicated to the JavaScript language itself, and not the parts that are specific to Web pages or other host environments. For information about APIs that are specific to Web pages, please see Web APIs and DOM.

The standards for JavaScript are the ECMAScript Language Specification (ECMA-262) and the ECMAScript Internationalization API specification (ECMA-402). As soon as one browser implements a feature, we try to document it. This means that cases where some proposals for new ECMAScript features have already been implemented in browsers, documentation and examples in MDN articles may use some of those new features. Most of the time, this happens between the stages 3 and 4, and is usually before the spec is officially published.

Do not confuse JavaScript with the Java programming languageJavaScript is not "Interpreted Java". Both "Java" and "JavaScript" are trademarks or registered trademarks of Oracle in the U.S. and other countries. However, the two programming languages have very different syntax, semantics, and use.

JavaScript documentation of core language features (pure ECMAScript, for the most part) includes the following:

For more information about JavaScript specifications and related technologies, see JavaScript technologies overview.

1. Variables and Data Types:

JavaScript uses variables to store and manipulate data. Variables can be declared using var, let, or const. Data types include strings, numbers, booleans, arrays, and objects.

let name = "John";
let age = 25;
let isStudent = true;
let fruits = ["apple", "banana", "orange"];
let person = { firstName: "John", lastName: "Doe" };

2. Operators:

JavaScript supports various operators for arithmetic, assignment, comparison, logical operations, and more.

let sum = 5 + 3;
let isEqual = (5 === "5"); // false (strict equality)
let logicalAnd = (true && false); // false

3. Control Flow:

JavaScript provides constructs for controlling the flow of your program, including if, else if, else, switch, for, while, and do-while statements.

if (condition) {
    // Code to execute if the condition is true
} else if (anotherCondition) {
    // Code to execute if anotherCondition is true
} else {
    // Code to execute if none of the conditions are true
}

for (let i = 0; i < 5; i++) {
    // Code to repeat 5 times
}

4. Functions:

Functions are blocks of reusable code that can be defined and called with or without parameters.

function greet(name) {
    return "Hello, " + name + "!";
}

let result = greet("John"); // "Hello, John!"

5. Objects and Arrays:

Objects and arrays are fundamental data structures in JavaScript.

let person = {
    firstName: "John",
    lastName: "Doe",
    age: 30,
    greet: function() {
        return "Hello, " + this.firstName + "!";
    }
};

let fruits = ["apple", "banana", "orange"];

6. DOM Manipulation:

JavaScript interacts with the Document Object Model (DOM) to manipulate HTML and CSS, allowing dynamic updates to web pages.

// Change the text content of an element with id="demo"
document.getElementById("demo").innerHTML = "Hello, World!";

// Add a new element to the document
let newElement = document.createElement("p");
newElement.textContent = "This is a new paragraph.";
document.body.appendChild(newElement);

7. Event Handling:

JavaScript enables you to respond to user interactions by handling events.

// Add a click event listener to a button
document.getElementById("myButton").addEventListener("click", function() {
    alert("Button clicked!");
});

8. AJAX and Fetch API:

Asynchronous JavaScript and XML (AJAX) and the Fetch API allow you to make asynchronous requests to servers.

// Using Fetch API to make a GET request
fetch('https://api.example.com/data')
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error('Error:', error));

9. ES6 Features:

Modern JavaScript includes features introduced in ECMAScript 2015 (ES6) and later versions, such as arrow functions, template literals, destructuring, and more.

// Arrow function
const add = (a, b) => a + b;

// Template literal
let name = "John";
console.log(`Hello, ${name}!`);

10. Modules and Classes:

JavaScript supports modular code organization and class-based object-oriented programming.

// Module: math.js
export function add(a, b) {
    return a + b;
}

// Importing module
import { add } from './math.js';

// Class
class Person {
    constructor(name) {
        this.name = name;
    }

    greet() {
        return `Hello, ${this.name}!`;
    }
}

JavaScript is a versatile language, and these basics provide a foundation for building interactive and dynamic web applications. As you progress, you may explore additional concepts such as closures, promises, async/await, and more advanced JavaScript frameworks and libraries.

Did you find this article valuable?

Support AM's blog by becoming a sponsor. Any amount is appreciated!