Our cookbook, Love Real Food, is here! Get your copy ↣

Learn JavaScript Online Free, Basics of JavaScript

Learn JavaScript Online, Basics of JavaScript

JavaScript is a programming language that enables web pages to be interactive. It is one of the pillars of the internet alongside HTML and CSS. This article will cover some basic JavaScript knowledge, including variables, data types, operators, control structures, functions, scopes, object-oriented JavaScript, event handling, and the basics of ECMAScript 6 (ES6).

Variables and Data Types

Variables in JavaScript are used to store data values. JavaScript uses the var, let, and const keywords to declare variables. A variable can store different types of data, each of which is classified as a ‘data type’. The primary data types in JavaScript are:

  • Number: Represents numeric values. e.g., let age = 25;
  • String: Represents sequence of characters. e.g., let name = "John";
  • Boolean: Represents logical entity and can have two values: true or false. e.g., let isApproved = true;
  • Object: Represents instance through which we can access members. e.g., let person = {firstName: "John", lastName: "Doe"};
  • Null: Represents no value or no object. e.g., let empty = null;
  • Undefined: Represents an uninitialized variable. e.g., let something;
Learn JavaScript Online, Basics of JavaScript

Operators and Control Structures

Operators are symbols that tell the JavaScript engine to perform specific mathematical or logical manipulations. For example, arithmetic operators are used to perform arithmetic operations (e.g., +, -, *, /, %).

Control structures help control the flow of the program’s execution. These include if-else statements, switch statements, loops, etc. An if-else statement, for example, performs different actions based on different conditions.

let grade = 85;

if (grade >= 90) {
    console.log("A");
} else if (grade >= 80) {
    console.log("B");
} else if (grade >= 70) {
    console.log("C");
} else {
    console.log("F");
}

Functions and Scopes

A function in JavaScript is a block of code designed to perform a specific task. A JavaScript function is executed when it is invoked (called). For example:

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

console.log(greet("John")); // Outputs: Hello, John

In JavaScript, scope refers to the visibility or accessibility of variables, functions, and objects in some particular part of your code during runtime. In other words, scope determines the accessibility (visibility) of these variables. Variables defined inside a function are not accessible (visible) from outside the function.

Learn JavaScript Online, Basics of JavaScript

Object-Oriented JavaScript

JavaScript is an object-based language. It provides a simple way to create objects using the literal notation or the new keyword. For example:

let car = {
    make: "Toyota",
    model: "Camry",
    year: 2021,
    start: function() {
        console.log(this.make + " " + this.model + " engine started!");
    }
}

car.start(); // Outputs: Toyota Camry engine started!

Event Handling

JavaScript is frequently used to create interactive web pages. Event handling refers to the process of handling events like clicks, mouse movements, keyboard input, etc. For example:

document.querySelector('button').addEventListener('click', function() {
    alert('Button was clicked!');
});

Basics of ECMAScript 6 (ES6)

ECMAScript 6, also known as ES6 and ECMAScript 2015, introduced significant updates to the JavaScript language. Some of the most important updates include classes, arrow functions, promises, etc.

Classes

ES6 introduced classes to JavaScript. A class is a type of function, but instead of using the keyword function, you use the keyword class.

class Car {
    constructor(make, model) {
        this.make = make;
        this.model = model;
    }

    start() {
        console.log(this.make + " " + this.model + " engine started!");
    }
}

let car = new Car("Toyota", "Camry");
car.start(); // Outputs: Toyota Camry engine started!
Learn JavaScript Online, Basics of JavaScript

Arrow Functions

ES6 introduced arrow functions, which provide a new way to write functions in JavaScript.

let greet = name => "Hello, " + name;
console.log(greet("John")); // Outputs: Hello, John

Promises

A Promise in JavaScript is an object representing the eventual completion or failure of an asynchronous operation.

let promise = new Promise((resolve, reject) => {
    let isDone = true;
    if(isDone) {
        resolve("The task is done.");
    } else {
        reject("The task is not done.");
    }
});

promise.then(
    successMessage => console.log(successMessage), // Outputs: The task is done.
    errorMessage => console.log(errorMessage)
);

In conclusion, JavaScript is a powerful language with many features. This article just scratches the surface of JavaScript. For those interested in web development, a solid understanding of JavaScript is essential.

Thank you for reading!

Dive into this insightful post on CodingReflex to unlock the power of Quarkus, Java’s revolutionary framework for building ultra-speed applications.

  • For real-time updates and insights, follow our tech enthusiast and expert, Maulik, on Twitter.
  • Explore a universe of knowledge, innovation, and growth on our homepage, your one-stop resource for everything tech-related.

For more information on related topics, check out the following articles:

Continue reading

Getting Started with TypeScript: TypeScript Tutorial
NEXT

Getting Started: TypeScript Tutorial

TypeScript, a superset of JavaScript developed and maintained by Microsoft, has increasingly become a go-to choice for modern web development. It incorporates static types, classes, interfaces, and other powerful features that can make JavaScript development more robust and efficient. Introduction […]
GoLang: Rising Star in the Programming World
PREVIOUS

GoLang: Rising Star in the Programming World

Golang is a powerful and versatile programming language that is gaining popularity among developers. This article answers some of the most common questions about Golang, including what it is used for, how it compares to other languages, and whether it is difficult to learn.

Our newsletter

Subscribe to our weekly newsletter & keep up with our latest recipes and organized workshops. You can unsubscribe at any time.

Error: Contact form not found.

You may also like these too

Popular now

Trending now