1. **Hello World**:
```javascript
console.log("Hello, World!");
```
This code prints "Hello, World!" to the browser console, which is a common way to display messages and debug JavaScript code.
2. **DOM Manipulation**:
```javascript
document.getElementById("myElement").innerHTML = "New Content";
```
Replace "myElement" with the ID of an HTML element. This code updates the content of the specified element with "New Content".
3. **Event Handling**:
```javascript
document.getElementById("myButton").addEventListener("click", function() {
alert("Button clicked!");
});
```
This code adds a click event listener to an HTML button with the ID "myButton." When the button is clicked, it triggers an alert with the message "Button clicked!".
4. **Conditional Statement**:
```javascript
var age = 25;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
```
This code checks the value of the `age` variable and logs a message based on whether the age is 18 or older.
5. **AJAX Request (Fetch API)**:
```javascript
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => {
console.log(data);
})
.catch(error => {
console.error('Error:', error);
});
```
This code makes an asynchronous HTTP GET request to 'https://api.example.com/data', retrieves JSON data, and logs it to the console. It also handles any errors that may occur during the request.
These JavaScript snippets cover a range of common tasks, including outputting text, interacting with the DOM, handling events, making AJAX requests, and using conditional statements. They serve as a foundation for more complex JavaScript development.
```javascript
console.log("Hello, World!");
```
This code prints "Hello, World!" to the browser console, which is a common way to display messages and debug JavaScript code.
2. **DOM Manipulation**:
```javascript
document.getElementById("myElement").innerHTML = "New Content";
```
Replace "myElement" with the ID of an HTML element. This code updates the content of the specified element with "New Content".
3. **Event Handling**:
```javascript
document.getElementById("myButton").addEventListener("click", function() {
alert("Button clicked!");
});
```
This code adds a click event listener to an HTML button with the ID "myButton." When the button is clicked, it triggers an alert with the message "Button clicked!".
4. **Conditional Statement**:
```javascript
var age = 25;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
```
This code checks the value of the `age` variable and logs a message based on whether the age is 18 or older.
5. **AJAX Request (Fetch API)**:
```javascript
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => {
console.log(data);
})
.catch(error => {
console.error('Error:', error);
});
```
This code makes an asynchronous HTTP GET request to 'https://api.example.com/data', retrieves JSON data, and logs it to the console. It also handles any errors that may occur during the request.
These JavaScript snippets cover a range of common tasks, including outputting text, interacting with the DOM, handling events, making AJAX requests, and using conditional statements. They serve as a foundation for more complex JavaScript development.