Sitemap

JavaScript Fundamentals

3 min readJul 14, 2026

--

Variable

Variable කියන්නේ මොකක්ද?

Variable එකක් කියන්නේ data එකක් memory එකේ store කරලා later access කරන්න දෙන name එකක්.

let name = "Hiruni";

Memory එකේ

Memory

name ------> "Hiruni"

name කියන්නේ reference (identifier).

JavaScript වල Variable Types

JavaScript වල තියෙන්නේ

var
let
const

Var :

var age = 20;

Properties

  • Function scoped
  • Can redeclare
  • Can reassign
  • Hoisted
var a = 10;

var a = 20;

console.log(a);

output :20

Problem with var

for (var i = 0; i < 3; i++) {}

console.log(i);

Output :3

Why?

Because var is function scoped, not block scoped.

Imagine a House 🏠

හිතන්න මේක තමයි code එක.

{
var name = "Hiruni";
}

ඔයා හිතන්නේ

“name මේ room එක ඇතුළේ විතරයි.”

නේද?

නමුත් var එහෙම නෙවෙයි.var කියන්නේ

“මම room එක ඇතුළේ නෙවෙයි, මුළු house එකේම ඉන්නවා.”

ඒ නිසා

{
var name = "Hiruni";
}

console.log(name);
Output:

Hiruni

let

Properties

  • Block scoped
  • Cannot redeclare
  • Can reassign
let age = 20;
 {
let name = "Hiruni";
}

console.log(name);

let කියන්නේ

“මම මේ room එක ඇතුළේ විතරයි.”

Room එකෙන් එළියට ආවම

output
: ReferenceError

Let වලදි redeclare කරන්න බෑ.

let a = 10;

a = 20;. works ✅

හැබැයි

let a = 10;
let a = 20; error 🔪

ඊලගට අපි කතාකරමු const ගැන.

Const

Properties

  • Block scoped
  • Cannot redeclare
  • Cannot reassign
const PI = 3.14;

හැබැයි අපිට බෑ

PI = 5;

පස්සෙ උඩ විදියට declare කරන්න.

Get Hiruni Liyanage’s stories in your inbox

Join Medium for free to get updates from this writer.

const object එකක් වැඩකරන්නෙ පහල විදියට 🪲

const user = {
name: "Hiruni"
};

user.name = "John";

Best practice එකක් විදියට Const use කරන්න බලන්න හැමතිස්සෙම.Values change වෙනවා නම් let. Var ගන්න එපා .

ඊලගට අපි කතාකරමු Data Types ගැන :

Primitive Data Types

String

Number

Boolean

Null

Undefined

BigInt

Symbol

Example

const age = 25;
age

↓

25

Non Primitive Data Types

Object

Array

Function

Example

const age = 25;
age

↓

25

01.Object :Object එකක් කියන්නේ related data එකක් එකට group කරලා තියන collection එකක්.

const person = {
name:"Hiruni"
age:"25"
}
person

↓

Address 1024

↓

{
name:"Hiruni"
}

මෙහි

person කියන්නේ Object එක.Object එක ඇතුළේ තියෙන්නේ

  • Properties (name, age, job)
  • Values (Hiruni, 20, Software Engineer)

Access කරන විදිහ :

console.log(person.name);

Hiruni

--------------------------------------

console.log(employee["name"]);
Hiruni

Property එක update කරන විදිහ :

person.age = 26;

Property එක add කරන විදිහ :

person.salary = 200000;
--------------------------------

{
name:"Hiruni",
age:26,
salary:200000
}

02. Array

Array එක කියන්නේ

multiple values order එකට store කරන collection එකක්.

const fruits = [
"Apple",
"Orange",
"Banana"
];

Memory :

0 → Apple

1 → Orange

2 → Banana
console.log(fruits[0]);
Apple

Add

fruits.push("Mango");

--------------------
Apple

Orange

Banana

Mango

Loop

fruits.forEach((fruit) => {
console.log(fruit);
});

------------------------------
Apple

Orange

Banana
const users = [
{ name: "Hiruni" },
{ name: "Ravee" }
];

-----------------------
users.map(user => (
<p>{user.name}</p>
))

Remember

Object

key → value

Array

index → value

3. Function

Function එක කියන්නේ , Reusable block of code.

function greet() {
console.log("Hello");
}
greet();

ඇයි Function එකත් Object එකක්?

JavaScript වල

function hello() {}

මේක ordinary code එකක් වගේ පේනවා.

නමුත් JavaScript Engine එකට

Function එකත්

Object එකක්

වගේ.

ඒ නිසා

function greet(){}

greet.language = "English";

console.log(greet.language);

----------------------------
English

Primitive — Stored by value.

Object — Stored by reference.

An AI Generated Image

--

--