JavaScript is a versatile programming language commonly used for web development.
It enables dynamic content, interactivity, and client-side scripting.
console.log("Hello, World!");
JavaScript is interpreted by web browsers and can interact with the DOM.
- Client-side scripting
- Event-driven programming
- Asynchronous behavior
Variables in JavaScript store data values.
There are three types of variable declarations:
var x = 10;
let y = 20;
const z = 30;
Use let
and const
instead of var
for better scoping.
var
- function-scopedlet
- block-scopedconst
- block-scoped, immutable
Functions are blocks of reusable code.
They can take parameters and return values.
function greet(name) { return "Hello, " + name; }
Functions help in code organization and modularity.
- Function declarations
- Function expressions
- Arrow functions
Loops allow repeated execution of code.
Common loops in JavaScript:
for(let i = 0; i < 5; i++) { console.log(i); }
while(condition) { /* code */ }
Loops help in handling iterative tasks efficiently.
- for loop
- while loop
- do-while loop
Objects in JavaScript are collections of key-value pairs.
They store structured data.
let person = { name: "John", age: 30 };
Objects can have methods and properties.
- Object literals
- Prototype-based inheritance
- Object manipulation