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-ஆ
undefined
assign ஆகும்.
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
👉 Local Scope
- Function Scope
- Block Scope
const
Keyword
const
variable → defined once, value change ஆகாது.- 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; |