Selected topic

Introduction to JavaScript

Basics

Prefer practical output? Use related tools below while reading.

What is JavaScript?

JavaScript is a high-level, dynamic language used for client-side scripting on the web. It allows you to add interactivity to your website by running scripts on the user's browser.

Basic Syntax

  • JavaScript code is written in a simple syntax, consisting of variables, data types, control structures (if/else statements), loops (for and while loops), functions, and object-oriented programming.
  • Statements are separated by semicolons (;).

Variables and Data Types

  • Variables are used to store values. In JavaScript, you can declare a variable using the let or const keywords.
  • There are several data types in JavaScript:
+ Numbers (e.g., 1, 2.5) + Strings (e.g., "Hello", 'World') + Booleans (true or false) + Arrays (e.g., [1, 2, 3]) + Objects (e.g., { name: "John", age: 30 })

Example

javascript
let myName = "John"; // declare a variable and assign it a string value
console.log(myName); // output: John

const PI = 3.14; // declare a constant and assign it a numeric value
console.log(PI); // output: 3.14

let scores = [80, 90, 70]; // declare an array variable and assign it some values
scores.push(95);
console.log(scores); // output: [80, 90, 70, 95]


Control Structures


  • If/Else Statements: Used to execute different code paths based on conditions.
javascript
let x = 5;
if (x > 10) {
console.log("x is greater than 10");
} else if (x < 10) {
console.log("x is less than 10");
} else {
console.log("x equals 10");
}

  • Loops: Used to execute code repeatedly.
javascript
for (let i = 0; i <= 5; i++) {
console.log(i);
}

let j = 0;
while (j < 5) {
console.log(j);
j++;
}


This is just a brief introduction to JavaScript basics. There's more to explore, but this should give you a good starting point!