Web Dev Day 6: JavaScript Guide

What is a Variable? A variable in JavaScript is a named container that holds a value. It allows us to store, update, and retrieve data dynamically. JavaScript provides three ways to declare variables: var (old syntax) let (modern) const (for constants) let name = "Bhupesh"; const age = 25; var country = "Canada"; Data Types in JS JavaScript has primitive and non-primitive data types. Primitive Data Types These are immutable and stored directly in memory: String → "Hello" Number → 42 Boolean → true, false Undefined → let x; (not assigned) Null → let y = null; (empty value) BigInt → 12345678901234567890123n Symbol → Symbol('unique') Non-Primitive Data Types These are reference types and stored as objects: Objects → { name: "John", age: 30 } Arrays → ["apple", "banana", "cherry"] Functions → function greet() { console.log("Hello!"); } Numbers in JavaScript JavaScript has only one type for numbers: floating-point numbers. Example: let a = 10; // Integer let b = 10.5; // Float let c = 1e3; // Scientific notation (1000) Special Number Values: Infinity → console.log(1 / 0); -Infinity → console.log(-1 / 0); NaN (Not-a-Number) → console.log("hello" * 2); Operations in JavaScript JavaScript supports the following operators: Arithmetic Operators: +, -, *, /, %, ** Comparison Operators: ==, ===, !=, !==, >, =,

Feb 9, 2025 - 02:12
 0
Web Dev Day 6: JavaScript Guide

What is a Variable?

A variable in JavaScript is a named container that holds a value. It allows us to store, update, and retrieve data dynamically.

JavaScript provides three ways to declare variables:

  • var (old syntax)
  • let (modern)
  • const (for constants)
let name = "Bhupesh"; 
const age = 25; 
var country = "Canada";

Data Types in JS

JavaScript has primitive and non-primitive data types.

Primitive Data Types

These are immutable and stored directly in memory:

  • String"Hello"
  • Number42
  • Booleantrue, false
  • Undefinedlet x; (not assigned)
  • Nulllet y = null; (empty value)
  • BigInt12345678901234567890123n
  • SymbolSymbol('unique')

Non-Primitive Data Types

These are reference types and stored as objects:

  • Objects{ name: "John", age: 30 }
  • Arrays["apple", "banana", "cherry"]
  • Functions
  function greet() { 
      console.log("Hello!"); 
  }

Numbers in JavaScript

JavaScript has only one type for numbers: floating-point numbers.

Example:

let a = 10;       // Integer
let b = 10.5;     // Float
let c = 1e3;      // Scientific notation (1000)

Special Number Values:

  • Infinity → console.log(1 / 0);
  • -Infinity → console.log(-1 / 0);
  • NaN (Not-a-Number) → console.log("hello" * 2);

Operations in JavaScript

JavaScript supports the following operators:

  • Arithmetic Operators: +, -, *, /, %, **
  • Comparison Operators: ==, ===, !=, !==, >, <, >=, <=
  • Logical Operators: &&, ||, !
  • Bitwise Operators: &, |, ^, ~, <<, >>
  • Ternary Operator: condition ? trueValue : falseValue

Example:

console.log(5 + 2);  // 7
console.log(10 / 2); // 5
console.log(3 ** 2); // 9 (Exponentiation)

NaN in JavaScript

NaN stands for "Not-a-Number." It occurs when an operation does not yield a valid number.

Example:

console.log("hello" * 2); // NaN
console.log(0 / 0);       // NaN

Operator Precedence in JavaScript

Operator precedence determines how expressions are evaluated in JavaScript.

Operator Type Operators Precedence
Parentheses () Highest
Exponentiation ** 2
Multiplication, Division, Modulus *, /, % 3
Addition, Subtraction +, - 4
Relational <, >, <=, >= 5
Equality ==, ===, !=, !== 6
Logical AND && 7
Logical OR `

Example:

{% raw %}

console.log(5 + 3 * 2);    // 11 (Multiplication first)
console.log((5 + 3) * 2);  // 16 (Parentheses first)

let Keyword

The let keyword was introduced in ES6. It has the following properties:

  • Block-scoped (Only accessible within the block {} it is declared in).
  • Can be reassigned but cannot be redeclared within the same scope.

Example:

let x = 10;
x = 20;  // ✅ Allowed

let x = 30; // ❌ Error (Cannot redeclare)

const Keyword

The const keyword has the following properties:

  • Block-scoped (Like let, it is confined within the block {} it is declared in).
  • Cannot be reassigned after declaration.
  • Cannot be redeclared in the same scope.
  • Must be initialized when declared.

Example:

const PI = 3.1416;
PI = 3.14; // ❌ Error (Cannot reassign)

var Keyword (Old Syntax)

The var keyword is function-scoped, meaning:

  • Can be redeclared and reassigned.
  • Not recommended in modern JavaScript due to scoping issues.

Example:

var name = "Alice";
var name = "Bob";  // ✅ Allowed (but not good practice)

Assignment Operators in JavaScript

Assignment operators are used to assign values to variables.

Operator Example Meaning
= x = 5 Assigns 5 to x
+= x += 2 x = x + 2
-= x -= 3 x = x - 3
*= x *= 4 x = x * 4
/= x /= 5 x = x / 5
%= x %= 2 x = x % 2