JavaScript Fundamentals: Variables, Data Types, Conversion, and Operators

1. Why Learn JavaScript?

  • To build web applications (e.g., e-commerce sites, dynamic web apps).

  • JavaScript powers front-end (React, Angular) and back-end (Node.js) development.

2. Variable Declarations: let, const, and var

KeywordScopeReassignable?Modern Usage
varFunctionYesAvoid (legacy)
letBlockYesPreferred for variables
constBlockNoPreferred for constants

Note: let/const are block-scoped and mitigate issues with var (e.g., global scope leaks).

3. Data Types in JavaScript

Primitive (Call by Value):

  • Number, String, Boolean, BigInt, Null, Undefined, Symbol.

  • Changes affect copies, not the original.

Non-Primitive (Call by Reference):

  • Array, Object, Function.

  • Changes affect the original reference.

4. Type Conversion

Explicit Conversion Examples:

let a = Number("123");  // 123 (primitive)  
let b = new Number("123");  // [Number: 123] (object)  
console.log(a === 123);  // true  
console.log(b === 123);  // false (object ≠ primitive)

Coercion Rules:

  • Number: "33"33, "33abc"NaN, true1.

  • Boolean: 1true, ""false, "hitesh"true.

String Concatenation Quirks:

javascript

Copy

Download

console.log(1 + "2");        // "12"  
console.log(1 + 2 + "2");    // "32" (left-to-right evaluation)

5. Comparison Operators

Loose vs. Strict Equality:

  • == (loose): Converts types ("2" == 2true).

  • === (strict): Checks type + value ("2" === 2false).

Null/Undefined Behaviors:

null > 0;    // false (null → 0 in comparisons)  
null >= 0;   // true  
null == 0;   // false (equality treats null/undefined specially)  
undefined == 0;  // false  

undefined always give the false

0
Subscribe to my newsletter

Read articles from Pramod Reddy Ambati directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pramod Reddy Ambati
Pramod Reddy Ambati