JavaScript Basics
1. Introduction to JavaScript: JavaScript is a scripting language used to add interactivity to web
pages. It can manipulate the DOM, handle events, and perform complex logic.
Example:
```javascript
console.log('Hello, World!');
```
2. Variables: JavaScript uses var, let, and const to declare variables. let and const are block-scoped,
while var is function-scoped.
Example:
```javascript
let age = 25;
const name = 'John';
var city = 'New York';
```
3. Data Types: JavaScript supports primitive types like number, string, boolean, undefined, and null.
Complex types include arrays and objects.
Example:
```javascript
let numbers = [1, 2, 3];
let person = { name: 'Alice', age: 30 };
```
4. Functions: Functions can be written as function declarations or arrow functions.
Example:
```javascript
function greet(name) {
return 'Hello, ' + name;
}
const greetArrow = (name) => `Hello, ${name}`;
```
5. Event Handling: Events like clicks, key presses, and form submissions can be handled using
event listeners.
Example:
```javascript
document.getElementById('myButton').addEventListener('click', function() {
alert('Button clicked!');
});
```
6. DOM Manipulation: JavaScript allows you to dynamically change HTML elements on a web page.
Example:
```javascript
document.getElementById('demo').innerHTML = 'Changed Text!';
```