Object Oriented Programming Articles

Page 12 of 589

JavaScript map() is not saving the new elements?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 166 Views

The map() method creates a new array with transformed elements but doesn't modify the original array. A common mistake is not assigning the returned value from map() to a variable. The Problem: Not Saving map() Results map() returns a new array, so you must capture its return value: let numbers = [1, 2, 3]; // Wrong: map() result is ignored numbers.map(x => x * 2); console.log(numbers); // Original array unchanged // Correct: assign the result let doubled = numbers.map(x => x * 2); console.log(doubled); // New array with transformed values [ ...

Read More

How to recognize when to use : or = in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 314 Views

The colon (:) is used to define properties in objects, while the equal sign (=) is used to assign values to variables. Understanding when to use each is fundamental in JavaScript. Using Colon (:) in Objects The colon separates property names from their values when creating object literals: var studentDetails = { "studentId": 101, "studentName": "John", "studentSubjectName": "Javascript", "studentCountryName": "US" }; console.log(studentDetails); { studentId: 101, studentName: 'John', studentSubjectName: 'Javascript', ...

Read More

Is the !! (not not) operator in JavaScript equivalent to reverse process of not operator?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 282 Views

Yes, the !! (not not) operator is the reverse process of the ! (not) operator. The single ! converts a value to its opposite boolean, while !! converts any value to its boolean equivalent. How the Not Operator Works The single ! operator converts truthy values to false and falsy values to true: var flag = true; console.log("Original value:", flag); console.log("Single ! result:", !flag); Original value: true Single ! result: false How the Not Not Operator Works The !! operator applies ! twice, effectively converting any value to its boolean ...

Read More

Using JSON.stringify() to display spread operator result?

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

The spread operator (...) allows you to expand objects and arrays into individual elements. When combining objects with the spread operator, you can use JSON.stringify() to display the merged result as a formatted string. Syntax // Object spread syntax var result = { ...object1, ...object2 }; // Convert to JSON string JSON.stringify(result); Example Here's how to use the spread operator to merge objects and display the result: var details1 = { name: 'John', age: 21 }; var details2 = { countryName: 'US', subjectName: 'JavaScript' }; var result = { ...details1, ...details2 ...

Read More

How to concatenate the string value length -1 in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 352 Views

In JavaScript, you can concatenate a string value (length-1) times using the Array() constructor with join(). This technique creates an array with empty elements and joins them with your desired string. Syntax new Array(count).join('string') Where count is the number of times you want to repeat the string, and the actual repetitions will be count - 1. How It Works When you create new Array(5), it generates an array with 5 empty slots. The join() method places the specified string between each element, resulting in 4 concatenated strings (length-1). Example var ...

Read More

How to generate array key using array index – JavaScript associative array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 377 Views

In JavaScript, you can create associative arrays (objects) using array indices as keys. This is useful for mapping array elements to their positions or creating key-value pairs from existing arrays. Using forEach() Method The forEach() method provides access to both the element and its index, allowing you to create dynamic object properties: var result = {}; var names = ['John', 'David', 'Mike', 'Sam', 'Bob', 'Adam']; names.forEach((nameObject, counter) => { var generatedValues = { [nameObject]: counter }; Object.assign(result, generatedValues); }); console.log(result); { John: 0, ...

Read More

Add time to string data/time - JavaScript?

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

In JavaScript, you can add time to a date/time string using the Date object's built-in methods. This is useful for calculating future times or adjusting existing timestamps. Basic Approach First, create a Date object from your string, then use methods like setHours(), setMinutes(), or setSeconds() combined with their getter counterparts to add time. var dateValue = new Date("2021-01-12 10:10:20"); dateValue.setHours(dateValue.getHours() + 2); // Add 2 hours Example: Adding Hours to Date String var dateValue = new Date("2021-01-12 10:10:20"); console.log("Original date: " + dateValue.toString()); // Add 2 hours dateValue.setHours(dateValue.getHours() + 2); ...

Read More

How to sort array by first item in subarray - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 580 Views

In JavaScript, you can sort an array of subarrays based on the first element of each subarray using the sort() method with a custom comparison function. The Problem Consider an array where each element is itself an array, and you want to sort by the first item in each subarray: var studentDetails = [ [89, "John"], [78, "Mary"], [94, "Alice"], [47, "Bob"], [33, "Carol"] ]; Sorting in Descending Order To sort by the ...

Read More

How to decrease size of a string by using preceding numbers - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 136 Views

Let's say our original string is the following with repeated letters − var values = "DDAAVIDMMMILLERRRRR"; We want to remove the repeated letters and precede letters with numbers. For this, use replace() along with regular expression. Syntax string.replace(/(.)\1+/g, match => match.length + match[0]) How It Works The regular expression /(.)\1+/g matches any character followed by one or more repetitions of the same character. The replacement function returns the count plus the original character. Example Following is the code − var values = "DDAAVIDMMMILLERRRRR"; var precedingNumbersInString = ...

Read More

ES6 Default Parameters in nested objects – JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 622 Views

ES6 allows you to set default parameters for nested objects using destructuring assignment. This feature helps handle cases where nested properties might be undefined or missing. Syntax function myFunction({ outerKey: { innerProperty = defaultValue, anotherProperty = anotherDefault } = {} } = {}) { // Function body } Example Here's how to implement default parameters in nested objects: function ...

Read More
Showing 111–120 of 5,881 articles
« Prev 1 10 11 12 13 14 589 Next »
Advertisements