
JavaScript New Updates 2025

JavaScript Update Fetch API with Error Handling Easy Merhod
In new Update of javascript we can
async function fetchData(url) {
try {
// Fetch data from the API
let response = await fetch(url);
// Check if the response is OK (status 200-299)
if (!response.ok) {
throw new Error(`HTTP Error! Status: ${response.status}`);
}
// Convert response to JSON
let data = await response.json();
console.log("Fetched Data:", data);
} catch (error) {
// Catch and log any errors
console.error("Error fetching data:", error.message);
}
}
// Example usage
fetchData("https://jsonplaceholder.typicode.com/posts");
JavaScript Update Debouncing to Optimize Performance
function debounce(func, delay) {
let timer;
return function (...args) {
// Clear the previous timer
clearTimeout(timer);
// Set a new timer to execute the function after delay
timer = setTimeout(() => func.apply(this, args), delay);
};
}
// Example: Optimize resize event handling
const handleResize = debounce(() => console.log("Window resized!"), 500);
// Listen for resize event
window.addEventListener("resize", handleResize);
JavaScript Update Copy Text to Clipboard Without Input Field
async function copyText(text) {
try {
// Use Clipboard API to copy text
await navigator.clipboard.writeText(text);
console.log("Copied to clipboard:", text);
} catch (err) {
console.error("Failed to copy text:", err);
}
}
// Example usage
copyText("Hello, Clipboard!");
JavaScript Update Detect User’s Dark Mode Preference
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
// Apply dark mode styles
document.body.style.backgroundColor = "#222";
document.body.style.color = "#fff";
console.log("Dark mode detected!");
} else {
console.log("Light mode detected!");
}
JavaScript Update Detect User’s Dark Mode Preference
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
// Apply dark mode styles
document.body.style.backgroundColor = "#222";
document.body.style.color = "#fff";
console.log("Dark mode detected!");
} else {
console.log("Light mode detected!");
}
JavaScript Update Shuffle an Array Efficiently
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
// Generate a random index
let j = Math.floor(Math.random() * (i + 1));
// Swap elements
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
// Example usage
console.log(shuffleArray([1, 2, 3, 4, 5]));