1. JavaScript Data Types
JavaScript is a dynamically typed language, which means you don’t need to specify the type of a variable explicitly.
Types of Data Types
Primitive Data Types
These store single values:
Number → let a = 10;
String→ let name = "John";
Boolean → let isActive = true;
Undefined → let x;
Null → let y = null;
BigInt → let big = 12345678901234567890n;
Symbol → Unique identifiers
Non-Primitive Data Types
These store collections of values:
Object
let person = {name: "John", age: 20};
Array
let numbers = [1, 2, 3, 4];
Function
function greet() {
console.log("Hello");
}
Conclusion:
Understanding data types helps you write better and error-free JavaScript code.
2. Compiler vs Interpreter
Programming languages use translators to convert code into machine language.
Compiler
- Translates entire code at once
- Faster execution after compilation
- Errors shown after full compilation
Example: C, C++
Interpreter
- Translates line by line
- Slower but easier to debug
- Stops immediately when error occurs
Example: JavaScript, Python
JavaScript Special Case
- JavaScript uses a Just-In-Time (JIT) Compiler, which is a mix of both compiler and interpreter.
- Engines like V8 JavaScript Engine convert JS into machine code efficiently.
Conclusion:
- JavaScript is interpreted but optimized using modern compilation techniques.
3. Variables in JavaScript
- Variables are used to store data values.
Ways to Declare Variables
✅ var
- Old method
- Function scoped
var x = 10;
✅ let
- Block scoped
- Can be updated
let age = 20;
age = 21;
✅ const
- Block scoped
- Cannot be changed
const PI = 3.14;
Best Practice:
Use let and const instead of var in modern JavaScript.
Example Programs
1.
let a = 10;
let b = a;
b = 20;
console.log(a);
console.log(b);
Output:
10
20
2.
let obj1 = {name: "John"};
let obj2 = obj1;
obj2.name = "David";
console.log(obj1.name);
Output:
David
Top comments (0)