var name = "John";
let age = 25;
const isStudent = true;
let number = 42;
let string = "Hello, World!";
let boolean = true;
let array = [1, 2, 3];
let object = {name: "John", age: 25};
let undefinedVariable;
let nullVariable = null;
let fruits = ["Apple", "Banana", "Cherry"];
fruits.push("Orange"); // Добавляет элемент в конец массива
console.log(fruits); // ["Apple", "Banana", "Cherry", "Orange"]
let firstFruit = fruits.shift(); // Удаляет первый элемент массива
console.log(firstFruit); // "Apple"
console.log(fruits); // ["Banana", "Cherry", "Orange"]
//Определение и вызов функций
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Alice")); // "Hello, Alice!"
//Функции стрелки (Arrow Functions)
const add = (a, b) => a + b;
console.log(add(2, 3)); // 5
//Условные операторы if, else if, else
let age = 18;
if (age < 18) {
console.log("You are a minor.");
} else if (age < 21) {
console.log("You are a young adult.");
} else {
console.log("You are an adult.");
}
//Тернарный оператор
let isMember = true;
let price = isMember ? 10 : 20;
console.log(price); // 10
//for
for (let i = 0; i < 5; i++) {
console.log(i);
}
//while
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
//do...while
let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
//Поиск элементов
let element = document.getElementById("myElement");
let elements = document.getElementsByClassName("myClass");
let elements = document.getElementsByTagName("p");
let element = document.querySelector(".myClass");
let elements = document.querySelectorAll(".myClass");
//Изменение элементов
let element = document.getElementById("myElement");
element.textContent = "New Text";
element.style.color = "red";
element.setAttribute("data-custom", "value");
//Создание и удаление элементов.
let newElement = document.createElement('div');
newElement.innerHTML = 'Hello, World!';
document.body.appendChild(newElement);
document.body.removeChild(newElement);
<button id="myButton">Click me</button>
<script>
document.getElementById('myButton').addEventListener('click', function() {
alert('Button clicked!');
});
</script>
<form id="myForm">
<input type="text" id="myInput" />
<button type="submit">Submit</button>
</form>
<script>
document.getElementById('myForm').addEventListener('submit', function(event) {
event.preventDefault();
alert(document.getElementById('myInput').value);
});
</script>
function fetchData(callback) {
setTimeout(() => {
callback('Data received');
}, 1000);
}
fetchData(function(data) {
console.log(data);
});
let promise = new Promise((resolve, reject) => {
let success = true;
if (success) {
resolve('Operation succeeded');
} else {
reject('Operation failed');
}
});
promise.then((message) => {
console.log(message);
}).catch((error) => {
console.error(error);
});
async function fetchData() {
try {
let response = await fetch('https://jsonplaceholder.typicode.com/posts/1');
let data = await response.json();
console.log(data);
} catch (error) {
console.error('Error:', error);
}
}
fetchData();
//Экспорт и импорт модулей
// file: math.js
export function add(a, b) {
return a + b;
}
// file: main.js
import { add } from './math.js';
console.log(add(2, 3));
try {
let result = riskyFunction();
} catch (error) {
console.error('An error occurred:', error);
} finally {
console.log('This will always run');
}
class CustomError extends Error {
constructor(message) {
super(message);
this.name = 'CustomError';
}
}
throw new CustomError('Something went wrong');
function createCounter() {
let count = 0;
return function() {
count++;
return count;
}
}
let counter = createCounter();
console.log(counter()); // 1
console.log(counter()); // 2
//Классы и наследование
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise.`);
}
}
class Dog extends Animal {
speak() {
console.log(`${this.name} barks.`);
}
}
let dog = new Dog('Rex');
dog.speak(); // Rex barks.
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
let xhr = new XMLHttpRequest();
xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts', true);
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 300) {
console.log(JSON.parse(xhr.responseText));
} else {
console.error('Request failed');
}
};
xhr.send();
function* generator() {
yield 1;
yield 2;
yield 3;
}
let gen = generator();
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // 3
let target = {
message1: "hello",
message2: "everyone"
};
let handler = {
get: function(target, prop, receiver) {
if (prop === "message1") {
return "world";
}
return Reflect.get(...arguments);
}
};