Javascript Articles

Page 230 of 534

How to Create Dark/Light Mode for a Website using JavaScript/jQuery?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 8K+ Views

Dark mode has become essential for modern websites, as it reduces eye strain and saves battery life on mobile devices. Studies show that 70-80% of users prefer dark mode, making it a crucial feature for user experience. In this tutorial, we'll learn to create a toggle between dark and light themes using JavaScript and jQuery. We'll use CSS classes and DOM manipulation to switch themes dynamically. Syntax The core method for toggling themes uses the classList.toggle() method: document.body.classList.toggle("dark-theme"); This adds the "dark-theme" class if it doesn't exist, or removes it if it does, ...

Read More

How to access an object having spaces in the object's key using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

When object keys contain spaces, you cannot use dot notation to access them. Instead, you must use bracket notation with quotes around the key name. The Problem with Spaces in Keys Dot notation requires valid JavaScript identifiers, which cannot contain spaces. Keys with spaces need bracket notation. const person = { 'first name': 'John', 'last name': 'Doe', age: 30 }; // This won't work - syntax error // console.log(person.first name); // This works - bracket notation console.log(person['first name']); console.log(person['last name']); ...

Read More

How to access the first value of an object using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 12K+ Views

In this article, you will understand how to access the first value of an object using JavaScript. The first value of an object refers to the value of the first property in the object. JavaScript provides several methods to retrieve this value, depending on whether you're working with objects or arrays. Using Object.values() for Objects The Object.values() method returns an array of all enumerable property values, making it easy to access the first value. const inputObject = {1: 'JavaScript', 2: 'Python', 3: 'HTML'}; console.log("A key-value pair object is defined and its values are: ", inputObject); ...

Read More

How to add a parameter to the URL in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 28K+ Views

In JavaScript, you can add parameters to URLs using the URLSearchParams API. There are two primary methods: append() for adding new key-value pairs and set() for adding or updating parameters. The append() method adds new key-value pairs to the URL without removing existing ones. If the same key already exists, it creates multiple entries. The set() method adds a parameter if it doesn't exist, or replaces the value if the key already exists. Unlike append(), it ensures only one value per key. Using append() Method The append() method adds new parameters while preserving existing ones: ...

Read More

How to create a dropdown list using JavaScript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 14K+ Views

We will learn to create a dropdown list using HTML and JavaScript below. Before starting with the article, let's understand the dropdown list and why we need to use it. The dropdown list gives multiple choices to users and allows them to select one value from all options. However, we can do the same thing using multiple radio buttons, but what if we have hundreds of choices? Then we can use the dropdown menu. When users click the dropdown button, it opens all the choices, and users can select anyone. Also, the dropdown provides a better user experience ...

Read More

How to create a dynamic length array with numbers and sum the numbers using JavaScript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 1K+ Views

In JavaScript, arrays are always dynamic in length. Unlike some programming languages, we don't need to define the array's length while creating it. We can create an array and push whatever number of elements we want. Here, we will create a dynamic length array with numbers and sum all its elements. Using For Loop to Sum Array Elements We can iterate over the array using a for loop and add every element to get the sum. Let's look at both traditional for loops and for-of loops. Syntax let arraySum = 0; for (let i = ...

Read More

How to add a property to a JavaScript object using a variable as the name?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 372 Views

In JavaScript, you can add properties to objects using either dot notation or bracket notation. However, when you need to use a variable as the property name, bracket notation is the only viable approach. The Problem with Dot Notation Dot notation treats everything after the dot as a literal property name, not a variable: let obj = {a: "value1"}; let propertyName = "dynamicProp"; // This creates a property literally named "propertyName" obj.propertyName = "value2"; console.log(obj); console.log("Property 'dynamicProp' exists:", obj.dynamicProp); // undefined { a: 'value1', propertyName: 'value2' } Property 'dynamicProp' exists: undefined ...

Read More

How to Create Query Parameters in JavaScript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 2K+ Views

Query parameters are key-value pairs appended to URLs after a question mark (?). They're essential for passing data between web pages, implementing search functionality, and creating dynamic URLs. For example, when you search on Amazon, your search term becomes a query parameter in the URL. JavaScript provides several methods to create query parameters programmatically. This is useful when building search features, filtering options, or any functionality that needs to modify URLs based on user input. Using encodeURIComponent() Method The encodeURIComponent() method encodes special characters in URL components. For example, spaces become %20, and other special characters are ...

Read More

How to calculate days left until next Christmas using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 923 Views

In this article, you will learn how to calculate the days remaining until the next Christmas using JavaScript. We'll use the Date object to work with dates and perform time calculations to determine how many days are left until December 25th. Understanding the Logic To calculate days until Christmas, we need to: Get today's date Determine the next Christmas date (this year or next year) Calculate the time difference in milliseconds Convert milliseconds to days Method 1: Direct Calculation In this approach, we calculate the time difference without using functions: let todayDate ...

Read More

How to access object properties from result returned by async() function in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

In this article, you will understand how to access object properties from result returned by async() functions in JavaScript. An object property in JavaScript is a variable that is associated with the object itself, i.e. the properties have a name and value is one of the attributes linked with the property. When working with async functions that return objects, you can access their properties using standard JavaScript notation once the promise resolves. The key is using await to wait for the promise to resolve before accessing properties. Using Dot Notation In this example, let's understand how to ...

Read More
Showing 2291–2300 of 5,340 articles
« Prev 1 228 229 230 231 232 534 Next »
Advertisements