JavaScript Summary
Introduction
JavaScript is a high-level, interpreted programming language primarily used for web
development. It enables dynamic content, interactivity, and client-side functionality in web
applications. It is supported by all modern web browsers and can be used alongside HTML
and CSS.
Key Features
1. Interpreted Language - Runs directly in the browser without compilation.
2. Lightweight - Designed for efficiency and speed.
3. Event-Driven - Executes code in response to user interactions.
4. Object-Oriented - Supports objects, prototypes, and classes.
5. Asynchronous - Uses callbacks, promises, and async/await for non-blocking operations.
Data Types
JavaScript has the following primitive data types:
- String
- Number
- Boolean
- Undefined
- Null
- Symbol
- BigInt
Variables
JavaScript uses `var`, `let`, and `const` to declare variables. Use `let` for block-scoped
variables and `const` for constants.
Functions
Functions in JavaScript can be defined using function expressions, function declarations,
and arrow functions.
Example:
```javascript
function add(a, b) {
return a + b;
}
```
Objects & Arrays
JavaScript supports objects and arrays for data storage and manipulation. Objects are key-
value pairs, while arrays are ordered lists.
Example:
```javascript
let person = {name: 'John', age: 30};
let numbers = [1, 2, 3, 4, 5];
```
DOM Manipulation
The Document Object Model (DOM) allows JavaScript to interact with HTML elements
dynamically.
Example:
```javascript
document.getElementById('demo').innerText = 'Hello, JavaScript!';
```
Event Handling
JavaScript handles user interactions using event listeners.
Example:
```javascript
document.getElementById('btn').addEventListener('click', function() {
alert('Button clicked!');
});
```
Asynchronous JavaScript
JavaScript supports asynchronous programming using:
- Callbacks
- Promises
- Async/Await
Example of a Promise:
```javascript
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve('Done!'), 1000);
});
promise.then(alert);
```