Intro ES6
ECMASCRIPT - for JavaScript standardization
The European Computer Manufacturers Association (ECMA)
-
Initially used ES5(2009) → Traditional way → Basic JavaScript → Only used for browsers.
ஆரம்பத்தில் JavaScript Browser-ல் மட்டும் பயன்படுத்தப்பட்டது. -
Later ES6 (2015) → introduced to embed the Google Chrome V8 engine in JavaScript.
2015-ல் ES6 வந்தபின், V8 Engine சேர்க்கப்பட்டது.
Use Cases
- Backend Development
- Mobile App Development
- Game Development
→ JavaScript இப்போ backend, mobile apps, game dev-க்கும் பயன்படுத்தப்படுகிறது.
ES6 Variable
- Temporary value வச்சு வைச்சுக்க variable.
(ஒரு data-வை temporary-ஆ வைக்க variable பயன்படுத்துவோம்.)
- In programming, a variable is a named container that stores data.
- It helps reuse and manipulation during program execution.
- For example:
let age = 25;stores the value 25 in a variable called age.
Syntax
var snacks = "Milk Bikis";
👉 Variable Type → var
👉 Variable Name → snacks
👉 Value → "Milk Bikis"
- Variable-க்கு value define செய்யாமல் விட்டால் default-ஆ
undefinedassign ஆகும்.
Variable Naming Conventions
- Start with lowercase (எப்போதும் small letter-ல ஆரம்பிக்கணும்)
- Variable name should be camelCase → example:
speedBreaker - Allowed start:
$,_(underscore) - Don’t start with numbers
JavaScript is Dynamically Typed
- JS ஒரு Dynamically Typed Language.
- Datatype (int, float, string etc.)-ஐ explicit-ஆ declare செய்ய வேண்டியதில்லை.
Scope in JavaScript
Types of Scope
- Global Scope
- Local Scope
Global Scope
- Variable-ஐ எங்கும் use பண்ணலாம் (function உள்ளும், block code உள்ளும்).
Local Scope
- Variable particular block உள்ளே மட்டுமே வேலை செய்யும்.
- அந்த block-க்கு வெளியே access பண்ண முடியாது.
👉 Function Scope → function உள்ளே.
👉 Block Scope → for loop, while loop உள்ளே.
-
Global Scope → Variable can be accessed anywhere in the program.
Example:var x = 10;(x is available everywhere). -
Local Scope → Variable can be accessed only inside a function or block.
Example:let y = 20;inside a function (y can’t be used outside).
var Keyword Drawbacks
var→ only supports function scope.- Block scope-ல use பண்ணினாலும் அது global + local இரண்டிலும் வேலை செய்கிறது.
let Keyword
- Supports both function & block scope.
- Can't redeclare allowed
- reassigned the value is allowed
👉 Local Scope
- Function Scope
- Block Scope
const Keyword
constvariable → defined once, value change ஆகாது.- Can't redeclared and reassigned the value
- Value & datatype இரண்டு change ஆகாது.
👉 Example in Array:
const array-க்குள் values push செய்யலாம், ஆனால் datatype & variable definition மாற்ற முடியாது.
Important Note
- If you didn't define them, they would be assigned to the global object
- If you use for var outside of a function, its belongs to the global scope
- If you use var inside of a function, its belongs to that function
- If use var inside of a block, i.e. a for loop, that variable is still available outside of that block.
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function scope | Block scope | Block scope |
| Re-declare | Allowed | Not allowed | Not allowed |
| Re-assign | Allowed | Allowed | Not allowed |
| Hoisting | Hoisted (undefined) | Hoisted (not initialized) | Hoisted (not initialized) |
| Example | var a = 5; |
let b = 10; |
const c = 15; |
A variable is just a container to hold data, and variable scope tells us where we can use it. If it’s in the global scope, we can access it anywhere, but if it’s local, it only works inside that function or block. Between the keywords, var has function scope and can be re-declared, let has block scope and can be reassigned, while const also has block scope but its value cannot change once assigned
Objects
🔹 Object Creation Methods
JS-ல் object உருவாக்க பல methods இருக்கு.
Common methods 👇
1️⃣ Constructor Object Method
const objectName = new Object();
2️⃣ Object Literal Syntax (Simple way)
const objectName = {};
👉 Example:
const person = {
firstName: "Arun",
age: 26
};
// Example of Object creation in object literal method
let car = {
make: 'Toyota',
model: 'Corolla',
year: 2020,
displayInfo: function () {
console.log(`This ${this.make} ${this.model} was manufactured in ${this.year}`);
}
}
car.displayInfo();
Syntax Explanation:
- Curly braces {} are used to denote the beginning and end of the object literal.
- Inside the braces, you specify key-value pairs separated by commas.
- Each key is a string (or a symbol in ES6+).
- Keys are followed by a colon : and then their corresponding values.
- Values can be of any data type, including strings, numbers, boolean, arrays, functions, or even other object
🔹 Object Basics
-
Object = key & value pairs.
-
Key = unique identifier.
-
Value = data (string, number, function etc).
👉 Example:
const person = {
firstName: "Arun",
age: 26
};
🔹 Object Properties & Methods
-
Object uniform properties & methods வைத்திருக்கும்.
-
Property values எந்த datatype ஆனாலும் இருக்கலாம் (string, number, function).
👉 Example:
const person = {
firstName: "Arun",
lastName: "Selvam",
fullName: function() {
console.log(this.firstName + " " + this.lastName);
}
};
this keyword
-
Inside an object method,
thisrefers to the object itself. -
So
this.firstNamemeans →person.firstName
this.firstName instead of person.firstName inside the method?
We use this.firstName instead of person.firstName so the method can work on any object that calls it, not just one fixed object. this gives flexibility and re-usability.
The this keyword doesn’t work in arrow functions because arrow functions don’t have their own this binding.
They inherit this from the surrounding (lexical) scope, unlike normal functions which get their own this based on how they are called.
🔹 Accessing Object Values
person.firstName; // property
person.fullName(); // function (method)
🔹 Template Literals with Object
👉 Backtick ` + ${} வைத்து values print செய்யலாம்.
const person = {
firstName: "Arun",
lastName: "Selvam",
fullName: function() {
return `${this.firstName} ${this.lastName}`;
}
};
🔹 Modern Method Shortcut
const person = {
firstName: "Arun",
lastName: "Selvam",
fullName() {
return `${this.firstName} ${this.lastName}`;
}
};
🔹 Object Merging
- இரண்டு object-ஐ ஒன்று சேர்க்கலாம்.
const person = { firstName: "Arun", age: 26 };
const personMethod = {
yearOfBirth() {
return new Date().getFullYear() - this.age;
}
};
👉 Merging Syntax
Object.assign(target, source);
Object.assign(person, personMethod);
🔹 Object Cloning
- Already இருக்கும் object-ஐ copy பண்ணலாம்.
Object.assign({}, source);
Object.assign({}, person);
✅ Modern Way → Spread Operator
const finalObject = { ...person, ...personMethod };
🔹 Delete Object Property / Method
delete objectName.key;
delete person.age;
🔹 Object Property Initializer
-
Properties key & value same இருந்தால் short form use பண்ணலாம்.
-
Property key மற்றும் value ஒரே பெயர்னா short-ஆ எழுதலாம்.
function getFullName(firstName, lastName) {
return {
firstName, // firstName : firstName
lastName, // lastName : lastName
}
}
🔹 Object Destructuring
-
Object-லிருந்து data-ஐ properties மூலம் எளிதாக எடுக்கலாம்.
-
Object destructuring எளிதாக data எடுக்க உதவும்.
const { key } = objectName;
Example:
const { firstName, lastName } = person;
✅ Default Value Assign
const { favColor = "red" } = person; // default value
✅ Rename Property
const { oldName: newName } = objectName;
const { firstName: myFirstName, lastName } = person;
✅ Rest Operator
const { key, ...rest } = objectName;
const { firstName, lastName, ...otherInfo } = person;