AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 417 of 840

How to import and export a module/library in JavaScript?

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

Note − To run this example you will need to run a localhost server. JavaScript modules allow you to break code into separate files and share functionality between them using export and import statements. This promotes code reusability and better organization. Example INDEX.html Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...

Read More

De-structuring an object without one key

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 213 Views

Object destructuring with the rest operator (...) allows you to extract specific properties while collecting the remaining properties into a new object. This technique is useful when you want to exclude certain keys from an object. Syntax const { keyToExclude, ...remainingKeys } = originalObject; Example: Excluding One Key Object Destructuring body { ...

Read More

Count unique elements in array without sorting JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 409 Views

When working with arrays containing duplicate values, counting unique elements is a common task. Let's explore different approaches to count unique elements in an array without sorting it first. Consider this sample array with duplicate values: const arr = ['Cat', 'Dog', 'Cat', 'Elephant', 'Dog', 'Grapes', 'Dog', 'Lion', 'Grapes', 'Lion']; console.log('Original array:', arr); Original array: ['Cat', 'Dog', 'Cat', 'Elephant', 'Dog', 'Grapes', 'Dog', 'Lion', 'Grapes', 'Lion'] Using Array.reduce() and lastIndexOf() This approach uses lastIndexOf() to identify the last occurrence of each element, ensuring each unique value is counted only once: const ...

Read More

JavaScript Create Submit button for form submission and another button to clear input

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

Form handling is a fundamental aspect of web development. A Submit button sends the form data for processing, while a Clear button allows users to reset or empty the form inputs. This functionality enhances user experience and ensures proper data entry. In this tutorial, we'll learn how to create a form with a Submit button for handling submissions and a Clear button for resetting inputs using JavaScript. We'll also handle the actions triggered by these buttons effectively. Creating a Submit Button for Form Submission ...

Read More

Make first letter of a string uppercase in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 507 Views

To make first letter of a string uppercase, use toUpperCase() in JavaScript. With that, we will use charAt(0) since we need to only capitalize the 1st letter. Syntax string.charAt(0).toUpperCase() + string.slice(1) Example function replaceWithTheCapitalLetter(values){ return values.charAt(0).toUpperCase() + values.slice(1); } var word = "javascript" console.log(replaceWithTheCapitalLetter(word)); Javascript Multiple Examples function capitalizeFirst(str) { return str.charAt(0).toUpperCase() + str.slice(1); } console.log(capitalizeFirst("hello world")); console.log(capitalizeFirst("programming")); console.log(capitalizeFirst("a")); console.log(capitalizeFirst("")); Hello world Programming A How It Works The method breaks ...

Read More

JavaScript undefined Property

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 246 Views

The JavaScript undefined property represents a value that indicates a variable has been declared but not assigned a value, or a property that doesn't exist. What is undefined? undefined is a primitive value automatically assigned to variables that are declared but not initialized, and to object properties that don't exist. Common Cases Where undefined Occurs // Declared but not assigned let age; console.log(age); // undefined // Function with no return value function greet() { console.log("Hello"); } let result = greet(); console.log(result); // undefined // Accessing non-existent object property let ...

Read More

Can we modify built-in object prototypes in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 215 Views

Yes, JavaScript allows modifying built-in object prototypes, but it should be done carefully. You can extend native objects like String, Array, or global functions like alert(). What are Built-in Object Prototypes? Built-in object prototypes are the foundation objects that JavaScript provides, such as String.prototype, Array.prototype, and global functions like alert(). Modifying them affects all instances of that type. Example: Overriding the alert() Function Custom Alert Function ...

Read More

Top n max value from array of object JavaScript

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

Finding the top n objects with maximum values from an array is a common programming task. Let's explore how to get objects with the highest duration values from an array of objects. Problem Statement Given an array of objects, we need to create a function that returns the top n objects based on the highest duration values. const arr = [ {"id":0, "start":0, "duration":117, "slide":4, "view":0}, {"id":0, "start":0, "duration":12, "slide":1, "view":0}, {"id":0, "start":0, "duration":41, "slide":2, "view":0}, {"id":0, "start":0, "duration":29, "slide":3, ...

Read More

JavaScript Remove all '+' from array wherein every element is preceded by a + sign

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 212 Views

When working with arrays containing strings that start with a '+' sign, you can remove these characters using JavaScript's map() method combined with replace(). Problem Statement Let's say we have an array where every element is preceded by a '+' sign: var studentNames = [ '+John Smith', '+David Miller', '+Carol Taylor', '+John Doe', '+Adam Smith' ]; Using map() with replace() The map() method creates a new array by calling a function on each element. We use replace() to ...

Read More

Display resultant array based on the object's order determined by the first array in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 130 Views

When working with objects and arrays in JavaScript, you often need to reorder data based on a specific sequence. This article shows how to use an array to determine the order of values extracted from an object using the map() method. The Problem Consider an object containing key-value pairs and an array that defines the desired order. We want to create a new array with values from the object, ordered according to the array sequence. // Object with key-value pairs var lastName = { "John": "Smith", "David": "Miller", ...

Read More
Showing 4161–4170 of 8,392 articles
« Prev 1 415 416 417 418 419 840 Next »
Advertisements