js content
- User
js content
- Assistant
js content
It seems you are looking for information or resources related to JavaScript (JS). Here are some fundamental concepts along with examples:
JavaScript allows you to declare variables using var, let, and const.
let name = "John"; // Allows reassignment
const age = 25; // Cannot be reassigned
var city = "New York"; // Function scoped or globally scoped
JavaScript has a variety of data types:
String, Number, Boolean, Null, Undefined, Symbol, BigIntlet str = "Hello"; // String
let num = 42; // Number
let isActive = true; // Boolean
let unknown = null; // Null
let notAssigned; // Undefined
Functions can be defined in multiple ways: function declarations, function expressions, and arrow functions.
// Function declaration
function greet(name) {
return `Hello, ${name}`;
}
// Function expression
const greetExpr = function(name) {
return `Hello, ${name}`;
};
// Arrow function
const greetArrow = (name) => `Hello, ${name}`;
JavaScript uses control flow statements like if, else, switch, for, while, and do...while.
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
for (let i = 0; i < 5; i++) {
console.log(i);
}
JavaScript objects can be created using object literals, constructors, or classes.
// Object literal
let person = {
name: "Jane",
age: 30,
greet: function() { return `Hello, ${this.name}`; }
};
// Using ES6 classes
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise.`);
}
}
const dog = new Animal('Dog');
dog.speak();
JavaScript provides several methods to manipulate arrays, such as map, filter, reduce, etc.
let numbers = [1, 2, 3, 4];
let doubled = numbers.map(num => num * 2); // [2, 4, 6, 8]
let evenNumbers = numbers.filter(num => num % 2 === 0); // [2, 4]
let sum = numbers.reduce((acc, num) => acc + num, 0); // 10
JavaScript handles asynchronous operations with Promises and the async/await syntax.
function fetchData() {
return new Promise((resolve) => {
setTimeout(() => {
resolve("Data loaded");
}, 2000);
});
}
// Using Promises
fetchData().then(data => console.log(data));
// Using Async/Await
async function getData() {
const data = await fetchData();
console.log(data);
}
getData();
You can manipulate the Document Object Model (DOM) with JavaScript.
const btn = document.querySelector('button');
btn.addEventListener('click', () => {
document.body.style.backgroundColor = 'blue';
});
JavaScript allows you to respond to user interactions with events.
document.addEventListener('click', (event) => {
alert(`You clicked: ${event.target.tagName}`);
});
JavaScript supports modular programming using import and export.
// Exporting
export const PI = 3.14;
export function add(x, y) {
return x + y;
}
// Importing
import { PI, add } from './math.js';
console.log(PI);
console.log(add(5, 10));
This overview provides a basic understanding of JavaScript. If you have specific topics or questions in mind, feel free to ask!
Not yet included in a published release.