DEV Community

Cover image for Basic JavaScript Through the Salaar Lens - Part 1
Pancharas Shubham
Pancharas Shubham

Posted on

Basic JavaScript Through the Salaar Lens - Part 1

Introduction

JavaScript is one of the most powerful programming languages used for web development. But what if we could make learning JavaScript as thrilling as the world of Salaar? By drawing parallels between JavaScript concepts and the epic saga of Salaar, we can make learning more engaging and memorable. In this guide, we'll explore JavaScript through the lens of Salaar, comparing programming principles with warriors, clans, and battle strategies.

console.log("Welcome to JavaScript, the Salaar way!");
Enter fullscreen mode Exit fullscreen mode

Story of var, let, and const in Salaar

Scope and Hoisting in the Kingdom of Khansaar

var – The Old Ruler of Khansaar

  • var is like the old ruling system of Khansaar—powerful but outdated.

  • It has function scope but no block scope, which can create unexpected results.

  • It is hoisted but initialized with undefined.

console.log(leader); // undefined (hoisted but not assigned)
var leader = "Old King of Khansaar";

Enter fullscreen mode Exit fullscreen mode

let – The Strategic War General

  • let is like Deva, the rising warrior who follows structured war strategies.

  • It has block scope, meaning it is only accessible within the block it is declared in.

  • Unlike var, it is hoisted but not initialized.

if (true) {
  let warPlan = "Attack at dawn";
  console.log(warPlan); // Accessible inside the block
}
console.log(warPlan); // Error: warPlan is not defined

Enter fullscreen mode Exit fullscreen mode

const – The Unshakable Khansaar Throne

  • const represents the Supreme Leader—unchanging and absolute.

  • It has block scope like let, but once assigned, its value cannot be changed.

const supremeLeader = "Khansaar King";
supremeLeader = "New King"; // Error: Cannot reassign a constant variable

Enter fullscreen mode Exit fullscreen mode

Best Practices / Important Notes

Use console.table instead of console.log

accountId = 46
accountEmail = "rajmanar_ka_salaar@gmail.com"
accountPassword = "Maa@143"
accountCity = "Tinsukia"
accountState = "Assam"

console.table([accountId, accountEmail, accountPassword, accountCity, accountState])
Enter fullscreen mode Exit fullscreen mode

Important Primitive Data Types

  1. String (Represents text)
let warrior = "Deva"; // Name of the warrior
Enter fullscreen mode Exit fullscreen mode

2.
Number (Represents numerical values)

let age = 35; // Deva's age
Enter fullscreen mode Exit fullscreen mode

3.
Boolean (Represents true/false values)

let isKaaparWarrior = true; // Deva belongs to Kaapar clan
Enter fullscreen mode Exit fullscreen mode

4.

Undefined (A variable that has been declared but not assigned a value)

let weapon;
console.log(weapon); // undefined (Deva's weapon is not assigned yet)
Enter fullscreen mode Exit fullscreen mode

5.
Null (Represents an empty or unknown value)

let throne = null;             // Throne of Khansaar is currently empty
console.log((throne));         // null 
console.log(typeof(throne));   // object
Enter fullscreen mode Exit fullscreen mode

6.

Symbol (Represents a unique value)

let uniqueID = Symbol("deva");
console.log(uniqueID); // Symbol(deva)
console.log(typeof(uniqueID)); // symbol
Enter fullscreen mode Exit fullscreen mode

7.

BigInt (Used for very large numbers beyond the safe integer limit)

let kingdomWealth = BigInt(9876543210123456789);
Enter fullscreen mode Exit fullscreen mode

Best Practices / Important Notes

Type of Null Datatype is object. Refer to Null Datatype.

console.log(typeof(throne));     // object
Enter fullscreen mode Exit fullscreen mode

"use strict"

Imagine Khansaar had a chaotic rule earlier where warriors could take any weapon without permission (non-strict mode). Now, with Rajamannar disciplined leadership (strict mode), every action is monitored and mistakes are punished.

"use strict";

warrior = "Deva"; // ❌ Error: warrior is not defined (undeclared variable)
console.log(warrior);
Enter fullscreen mode Exit fullscreen mode

Without "use strict", JavaScript would assume warrior is a global variable and run without error. But in strict mode, it prevents such mistakes, enforcing proper variable declarations.

Data Types Conversions

1.
String to Number

Deva wants to convert his warrior rank (stored as a string) into a number.

let rank = "1"; 
let numericRank = Number(rank);  
console.log(numericRank); // Output: 1

Enter fullscreen mode Exit fullscreen mode

2.
Number to String

Vardha wants to convert his power level into a string.

let powerLevel = 9000;
let powerStr = String(powerLevel);
console.log(powerStr); // Output: "9000"
Enter fullscreen mode Exit fullscreen mode

3.
Boolean to Number

A warrior’s loyalty (true or false) is converted into a number.

let isLoyal = true;
let loyalNumber = Number(isLoyal);
console.log(loyalNumber); // Output: 1
Enter fullscreen mode Exit fullscreen mode

4.
Number to Boolean

If an army size is 0, it means no warriors. Any non-zero value means they exist.

let armySize = 100;
let hasArmy = Boolean(armySize);
console.log(hasArmy); // Output: true

Enter fullscreen mode Exit fullscreen mode

5.
String to Boolean

A non-empty string means a warrior exists.

let warriorName = "Deva";
console.log(Boolean(warriorName)); // Output: true

let emptyName = "";
console.log(Boolean(emptyName)); // Output: false
Enter fullscreen mode Exit fullscreen mode

`

Top comments (0)