JavaScript · Lesson 28 / 32 · தமிழ்
Sets
கருத்தைப் புரிந்துகொள்ளுங்கள். Code-ஐ மாற்றி இயக்குங்கள். ஒரு சிறிய பயிற்சியால் கற்றதை உறுதி செய்யுங்கள்.
மூலப் பாட விளக்கங்கள் தமிழில் உள்ளன. கூடுதல் concept, practice மற்றும் answer notes ஆங்கிலத்தில் உள்ளன.
முக்கியக் கருத்து
Set stores unique values. It is useful for deduplicating primitive values and checking membership; objects are compared by identity, not by matching properties.
Learning goal: Explain what the example does, predict its result, and change it to solve the practice task.
விரிவான விளக்கங்கள்
JavaScript-ல Set
Set என்பது unique values மட்டுமே இருக்குற collection. Duplicates automatically remove ஆகும், insertion order maintain ஆகும்.
const set = new Set([1, 2, 3, 2, 1]);
console.log(set); // Set { 1, 2, 3 } — duplicates போயிடும்
set.add(4);
set.add(3); // ignored — already exists
set.delete(1);
set.has(2); // true
set.size; // 3
// Array-க்கு convert
const arr = [...set];
// Most common use: duplicates remove பண்ண
const nums = [1, 2, 2, 3, 3, 4];
const unique = [...new Set(nums)]; // [1, 2, 3, 4]Set Operations
const a = new Set([1, 2, 3, 4]);
const b = new Set([3, 4, 5, 6]);
// Union — இரண்டிலும் இருக்குறவை எல்லாம்
const union = new Set([...a, ...b]);
// Intersection — இரண்டிலும் உள்ளவை
const intersection = new Set([...a].filter(x => b.has(x)));
// Difference — a-ல மட்டும் இருக்குறவை
const difference = new Set([...a].filter(x => !b.has(x)));முழுமையான உதாரணம்
Run this complete index.js program in the JavaScript console editor below. The earlier reference fragments may need a browser page or other surrounding setup.
const topics = new Set(["arrays", "objects", "arrays"]);
topics.add("functions");
console.log(topics.size);
console.log(topics.has("arrays"));
console.log([...topics].join(", "));எதிர்பார்க்கும் முடிவைப் பாருங்கள்
3 true arrays, objects, functions
மாற்றி இயக்கிப் பாருங்கள்
Load the example, change a value, predict the output, and run it again. Use the console output to check your reasoning.
Editor load ஆனதும் code-ஐ ஏற்றி Run அழுத்துங்கள்.
OneCompiler என்ற இலவச வெளிப்புற சேவை code-ஐ இயக்குகிறது. Run செய்யும் code அங்கு அனுப்பப்படும். தனிப்பட்ட தகவல்கள் அல்லது secret keys உள்ளிட வேண்டாம்.
சிறிய பயிற்சி
Practice challenge
Delete objects and predict the remaining size.
விடை மற்றும் விளக்கம்
topics.delete("objects") removes that member and size becomes 2.
தவிர்க்க வேண்டிய தவறு: Two separate objects such as {id: 1} and {id: 1} remain different Set members.
அடுத்த பாடத்திற்குத் தயாரா?
- Explain the example in your own words.
- Complete the challenge before revealing its answer.
- Describe the common mistake and how to avoid it.