Basics of JavaScript

Home | Blog | Computer Programming | Basics of JavaScript

Published:

by

JavaScript is one of the core technologies of web development, alongside HTML and CSS. It is a high-level, interpreted programming language that is primarily used to create interactive and dynamic web content. Here’s a breakdown of the basics of JavaScript:

1. Syntax and Structure

  • Case Sensitivity: JavaScript is case-sensitive. For example, myVariable and myvariable are different.
  • Statements: Each JavaScript instruction is called a statement, typically ending with a semicolon ;.
  • Comments:
    • Single-line comments use //.
    • Multi-line comments use /* ... */.

2. Variables

Variables are containers for storing data values. In JavaScript, you can declare variables using:

  • var: Old way of declaring variables, has function scope.
  • let: Declares block-scoped variables.
  • const: Declares block-scoped constants, meaning they cannot be reassigned. Example:

3. Data Types

JavaScript is a loosely typed or dynamic language, meaning a variable can hold any type of value:

  • Primitive Types:
    • String: Textual data, e.g., "Hello".
    • Number: Numeric data, both integers and floating-point, e.g., 42 or 3.14.
    • Boolean: Represents logical values, true or false.
    • Undefined: A variable that has been declared but not yet assigned a value.
    • Null: Represents the intentional absence of any object value.
    • Symbol: Unique and immutable primitive values, introduced in ES6.
    Example:

4. Operators

JavaScript includes a wide range of operators to perform operations on variables and values:

  • Arithmetic Operators: +, -, *, /, % (modulus)
  • Assignment Operators: =, +=, -=, *=, /=
  • Comparison Operators: ==, ===, !=, !==, <, >, <=, >=
  • Logical Operators: && (AND), || (OR), ! (NOT) Example:
   let x = 10;
   let y = 5;
   let sum = x + y;  // 15

5. Control Structures

JavaScript provides several ways to control the flow of the program:

  • Conditional Statements: if, else if, else, switch.
  • Loops: for, while, do...while. Example:

6. Functions

Functions are reusable blocks of code that can be executed when called. A function can take parameters and return a value.

Function Declaration:

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

} 
console.log(greet("Alice")); // Output: "Hello, Alice"

Arrow Functions (ES6):

const add = (a, b) => a + b; console.log(add(3, 4)); // Output: 7

7. Objects

JavaScript is an object-oriented language. Objects are collections of key-value pairs, and they can store multiple values or functions.

Example:

8. Arrays

Arrays in JavaScript are used to store multiple values in a single variable. Arrays are zero-indexed, meaning the first element has an index of 0.

Example:

9. Events and DOM Manipulation

JavaScript is widely used to handle user events, such as clicks, and to interact with the Document Object Model (DOM) of a webpage.

Accessing DOM Elements:

let element = document.getElementById("myButton"); element.innerHTML = "Click Me!";

Event Listeners:

document.getElementById("myButton").addEventListener("click", function() { alert("Button clicked!"); });

10. ES6+ Features

Modern JavaScript (ES6 and beyond) introduces several new features, including:

  • Let and Const: Block-scoped variables.
  • Arrow Functions: Shorter syntax for functions.
  • Template Literals: Backticks “ allow for string interpolation.
  • Classes: Simplified syntax for object-oriented programming.
  • Modules: Ability to export and import code between files. Example of a class:

Conclusion

JavaScript is a versatile language with applications ranging from client-side (in browsers) to server-side (via Node.js). Understanding its basic syntax, control structures, functions, objects, and modern features is key to becoming proficient in web development.

Use this online coding tool for free.