Demystifying Variables and Data Types: The Backbone of JavaScript Programming

Demystifying Variables and Data Types: The Backbone of JavaScript Programming

Learn JavaScript from Scratch - The Complete Guide for Beginners

Declaring variables:

  • In JavaScript, you can declare variables using three different keywords: var, let, and const.

  • var was the original way to declare variables in JavaScript, but it has some issues with scoping and hoisting.

  • let and const were introduced in ES6 (ECMAScript 2015) and are the recommended ways to declare variables nowadays.

// Using var
var x = 5;

// Using let
let y = 10;

// Using const
const z = 15;
  • The main difference between let and const is that let allows you to reassign the value of the variable, while const declares a constant value that cannot be reassigned.

Data types:

  • In JavaScript, there are two main categories of data types: primitive and non-primitive.

Primitive data types:

  • Primitive data types are immutable, meaning their values cannot be changed once they are created.

  • There are seven primitive data types in JavaScript:

Number: Used to represent both integers and floating-point numbers.

// Number
let age = 25;

// Number
let price = 9.99;

String: Used to represent textual data.

// String
let name = "John Doe";

Boolean: Used to represent logical values, either true or false.

// Boolean
let isStudent = true;

Undefined: Represents an uninitialized variable or a variable with no value assigned.

// Value is undefined
let x;

Null: Represents a non-existent or invalid value.

// Explicitly assigning null
let y = null;

BigInt: Introduced in ES2020, used to represent integers of arbitrary length.

// BigInt
let bigNumber = 12345678901234567890n;

Symbol: Introduced in ES6, used to create unique and immutable identifiers.

// Symbol
let id = Symbol("id");

Non-primitive data types:

  • Non-primitive data types are mutable, meaning their values can be changed after they are created.

  • There are three main non-primitive data types in JavaScript:

Object: Used to store collections of key-value pairs.

// Object
let person = {
  name: "Alice",
  age: 30
};

Array: Used to store ordered collections of values.

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

Function: Represents a reusable block of code that can be called with arguments and can optionally return a value.

// Function
function greet(name) {
  console.log(`Hello, ${name}!`);
}

Type conversion and type coercion:

  • Type conversion is the explicit conversion of one data type to another.

  • Type coercion is the implicit conversion of data types by the JavaScript engine.

// Type conversion
let num = Number("42"); // Converting string to number
let str = String(true); // Converting boolean to string// Type coercion

// Type coercion
console.log(3 + "4"); // Output: "34" (string concatenation)
console.log("5" - 2); // Output: 3 (string to number coercion)

Operators:

  • JavaScript provides various operators to perform operations on values and variables.

  • Arithmetic operators: +, -, /, %, *, **(exponentiation)

  • Assignment operators: =, +=, =, =, /=, %=, *=

  • Comparison operators: ==, ===, !=, !==, >, <, >=, <=

  • Logical operators: && (and), || (or), ! (not)

  • And many more, like bitwise operators, ternary operator, etc.

let a = 5;
let b = 3;

// Arithmetic operations
console.log(a + b); // Output: 8
console.log(a - b); // Output: 2
console.log(a * b); // Output: 15
console.log(a / b); // Output: 1.6666666666666667
console.log(a % b); // Output: 2

// Assignment operators
a += b; // Same as a = a + b
console.log(a); // Output: 8 

// Comparison operators
console.log(a > b); // Output: true
console.log(a === 8); // Output: true

// Logical operators
let isEligible = true;
let isEmployed = false;
console.log(isEligible || isEmployed); // Output: true
console.log(!isEligible); // Output: false

Now that you have a solid understanding of variables and data types in JavaScript, it’s time to dive into another crucial aspect of programming: control flow. In the next article, we’ll explore conditional statements like if, else, and switch, as well as different types of loops, which are essential for executing code based on certain conditions and creating dynamic and interactive programs.