Javascript Fundamentals
Javascript Fundamentals
Function-scoped.
function exampleVar() {
console.log(x); // undefined
var x = 10;
console.log(x); // 10
}
exampleVar();
let
Block-scoped.
function exampleLet() {
console.log(y); // ReferenceError: Cannot access 'y' befo
re initialization
let y = 20;
console.log(y); // 20
}
exampleLet();
const
Block-scoped.
Javascript Fundamentals 1
Cannot be redeclared or updated.
function exampleConst() {
const z = 30;
console.log(z); // 30
z = 40; // TypeError: Assignment to constant variable.
}
exampleConst();
2. DOM Manipulation
Accessing elements and modifying their properties or content.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DOM Manipulation</title>
</head>
<body>
<p id="myParagraph">Original Text</p>
<script>
const paragraph = document.getElementById('myParagrap
h');
paragraph.textContent = 'Changed Text';
</script>
</body>
</html>
document.getElementById('myParagraph').style.color = 'blue';
Javascript Fundamentals 2
3. JIT (Just-In-Time) Compilation
JavaScript engines (like V8 in Chrome) compile JavaScript code into machine
code at runtime.
Variable Object
Scope Chain
this keyword
function first() {
console.log('First');
second();
console.log('First again');
}
function second() {
console.log('Second');
}
first();
// Output:
// First
// Second
// First again
5. Event Listeners
Allow you to run code when events are triggered (e.g., clicks, keypresses).
Javascript Fundamentals 3
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Event Listeners</title>
</head>
<body>
<button id="myButton">Click Me</button>
<script>
const button = document.getElementById('myButton');
button.addEventListener('click', () => {
alert('Button was clicked!');
});
</script>
</body>
</html>
6. == vs ===
== : Equality operator. Compares values for equality after converting both
values to a common type.
: Strict equality operator. Compares both value and type without type
===
conversion.
Examples:
Summary
Use let and const for block-scoped variables.
Javascript Fundamentals 4
JIT compilation improves performance by compiling code at runtime.
Understand the execution context and call stack for better debugging.
Javascript Fundamentals 5