[javascript]

JavaScript is a high-level, interpreted programming language that is primarily used for adding interactivity to websites. In this blog post, we will cover the basics of JavaScript to help you get started with this powerful language.

Table of Contents

Variables

In JavaScript, you can use the var, let, or const keywords to declare variables. The var keyword has been used traditionally, but let and const were introduced in ECMAScript 6.

var age = 25;   // Variable declared using var keyword
let name = "John";   // Variable declared using let keyword
const PI = 3.14;   // Variable declared using const keyword

Data Types

JavaScript has several built-in data types, including numbers, strings, booleans, objects, arrays, and more.

let age = 25;   // Number
let name = "John";   // String
let isStudent = true;   // Boolean
let person = { name: "John", age: 25 };   // Object
let colors = ["red", "green", "blue"];   // Array

Operators

JavaScript provides a wide range of operators for performing mathematical, logical, and comparison operations. Here are a few examples:

let x = 10;
let y = 5;

let sum = x + y;   // Addition
let difference = x - y;   // Subtraction
let product = x * y;   // Multiplication
let quotient = x / y;   // Division
let isGreater = x > y;   // Greater than
let isEqual = x === y;   // Equal to

Control Flow

You can use control flow statements such as if/else and for loops to control the flow of your program.

let age = 18;

if (age >= 18) {
    console.log("You are an adult.");
} else {
    console.log("You are a minor.");
}

for (let i = 0; i < 5; i++) {
    console.log(i);
}

Functions

Functions are reusable blocks of code that perform specific tasks. You can define functions using the function keyword.

function square(number) {
    return number ** 2;
}

let result = square(5);   // 25

Objects

JavaScript objects are collections of key-value pairs. You can define objects using object literal notation.

let person = {
    name: "John",
    age: 25,
    occupation: "Developer"
};

console.log(person.name);   // John
console.log(person.age);   // 25

Arrays

Arrays are ordered collections of values. You can define arrays using square brackets.

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

console.log(fruits[0]);   // apple
console.log(fruits.length);   // 3

Conclusion

This blog post provided a brief introduction to the basics of JavaScript. With these fundamental concepts, you can start exploring more advanced topics in JavaScript and build powerful web applications. Happy coding!

References: