Obafemi Emmanuel

JavaScript Basics

Published 2 months ago

JavaScript is a fundamental language for web development. Whether you are a beginner or looking for a refresher, understanding JavaScript basics is crucial. In this guide, we will cover variables, data types, type conversion, operators, and comments in JavaScript.


1. Variables in JavaScript

Variables store data values. JavaScript provides three ways to declare variables:

  • var – Function-scoped and can be redeclared.
  • let – Block-scoped and cannot be redeclared in the same scope.
  • const – Block-scoped and must be assigned a value at declaration.

Example:

var x = 10;  // Function-scoped
let y = 20;  // Block-scoped
const z = 30; // Constant value

2. Data Types in JavaScript

JavaScript has several data types, categorized into primitive and non-primitive (reference) types.


Primitive Data Types:

  1. String – Represents text values.
let name = "John";
  1. Number – Represents both integers and floating-point numbers.
let age = 25;
  1. Boolean – Represents true or false.
let isAdmin = true;
  1. Null – Represents an empty or unknown value.
let value = null;
  1. Undefined – A variable that has been declared but not assigned a value.
let noValue;
  1. Symbol – Unique and immutable value.
let sym = Symbol("id");

Non-Primitive (Reference) Data Type:

  • Object – Stores key-value pairs and can contain functions and other data types.
let person = {
    firstName: "John",
    age: 30
};

3. Type Conversion and Type Coercion

Type Conversion (Explicit)

Changing one data type to another manually:

let num = String(100); // Converts number to string
let str = Number("123"); // Converts string to number

Type Coercion (Implicit)

JavaScript automatically converts data types when necessary:

console.log("5" + 3);  // Output: "53" (String Concatenation)
console.log("5" - 2);  // Output: 3 (String converted to Number)

4. Operators in JavaScript

Arithmetic Operators:

Perform mathematical operations.

let sum = 10 + 5;  // Addition
let diff = 10 - 5; // Subtraction
let product = 10 * 5; // Multiplication
let quotient = 10 / 5; // Division
let remainder = 10 % 3; // Modulus
let power = 2 ** 3; // Exponentiation

Assignment Operators:

Assign values to variables.

let x = 10;
x += 5;  // Equivalent to x = x + 5;
x *= 2;  // Equivalent to x = x * 2;

Comparison Operators:

Compare two values and return a Boolean.

console.log(5 == "5");   // true (loose comparison)
console.log(5 === "5");  // false (strict comparison)
console.log(5 > 3);      // true

Logical Operators:

Used for boolean logic.

console.log(true && false);  // false (AND)
console.log(true || false);  // true (OR)
console.log(!true);          // false (NOT)

Bitwise Operators:

Perform bit-level operations.

console.log(5 & 1); // Bitwise AND
console.log(5 | 1); // Bitwise OR
console.log(5 ^ 1); // Bitwise XOR

5. Comments in JavaScript

Comments make the code more readable and are ignored by the JavaScript engine.


Single-line Comment:

// This is a single-line comment

Multi-line Comment:

/*
 This is a multi-line comment
 spanning multiple lines.
*/

Conclusion

Understanding these JavaScript basics is crucial for building web applications. Mastering variables, data types, operators, and comments will provide a solid foundation for learning advanced JavaScript concepts. Keep practicing, and happy coding!


Leave a Comment


Choose Colour