Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 162 of 801
Using the get in JavaScript to make getter function
The get keyword in JavaScript creates a getter method that allows you to access object properties like regular properties while executing custom logic behind the scenes. Syntax const obj = { get propertyName() { // Custom logic return value; } }; Basic Example const studentDetails = { _name: "David Miller", get studentName() { ...
Read MoreFetch alternative even values from a JavaScript array?
To fetch alternative even values from a JavaScript array, you need to iterate through the array and check if the index is even using the modulo operator. An index is even when index % 2 == 0. Syntax if (index % 2 == 0) { // This will select elements at even positions (0, 2, 4, ...) } Example The following example demonstrates how to fetch alternative even values from an array: var subjectsName = ["MySQL", "JavaScript", "MongoDB", "C", "C++", "Java"]; for (let index = 0; index ...
Read MoreObject comparison Complexity in JavaScript using comparison operator or JSON.stringlify()?
In JavaScript, comparing objects using comparison operators (== or ===) checks reference equality, not content equality. For content comparison, JSON.stringify() can be used with limitations. The Problem with Comparison Operators When comparing objects with == or ===, JavaScript checks if both variables reference the same object in memory, not if their contents are identical: var object1 = { firstName: "David" }; var object2 = { firstName: "David" }; var object3 = object1; console.log("object1 == object2:", object1 == object2); // false - different objects console.log("object1 === object2:", object1 === object2); // false - different ...
Read MoreVariable defined with a reserved word const can be changed - JavaScript?
In JavaScript, variables declared with const cannot be reassigned, but this doesn't mean the contents of objects or arrays are immutable. You can still modify the properties of objects declared with const. Understanding const with Objects The const keyword prevents reassignment of the variable itself, but object properties can still be modified: const details = { firstName: 'David', subjectDetails: { subjectName: 'JavaScript' } }; // This works - modifying object properties details.firstName = 'John'; details.subjectDetails.subjectName = 'Python'; console.log(details); { firstName: 'John', subjectDetails: { ...
Read MoreHow do I turn a string in dot notation into a nested object with a value – JavaScript?
Converting a dot-notation string into a nested object is a common task in JavaScript. This approach uses split() and map() to dynamically build the nested structure. Problem Setup Let's say we have a dot-notation string representing nested property keys: const keys = "details1.details2.details3.details4.details5"; const firstName = "David"; We want to create a nested object where the final property gets the value "David". Solution Using split() and map() The technique involves splitting the dot-notation string and using map() to build nested objects: const keys = "details1.details2.details3.details4.details5"; const firstName = "David"; ...
Read MoreDisplay the result difference while using ceil() in JavaScript?
The Math.ceil() function rounds a number UP to the nearest integer. While division might give decimal results, Math.ceil() ensures you get the next whole number. How Math.ceil() Works If your value is 4.5, 4.3, or even 4.1, Math.ceil() will return 5. It always rounds UP, unlike regular rounding. Example: Division Without ceil() Let's see normal division first: var result1 = 98; var result2 = 5; console.log("The actual result is=" + result1 / result2); The actual result is=19.6 Example: Division With ceil() Now let's apply Math.ceil() to round ...
Read MoreHow do I make an array with unique elements (remove duplicates) - JavaScript?
In JavaScript, there are several ways to remove duplicate elements from an array and create a new array with unique values only. Using filter() with indexOf() The filter() method combined with indexOf() is a traditional approach that keeps only the first occurrence of each element: var duplicateNumbers = [10, 20, 100, 40, 20, 10, 100, 1000]; console.log("Original array:"); console.log(duplicateNumbers); var uniqueNumbers = duplicateNumbers.filter(function (value, index, array) { return array.indexOf(value) === index; }); console.log("Array with unique elements:"); console.log(uniqueNumbers); Original array: [ 10, 20, 100, ...
Read MoreHow to replace some preceding characters with a constant 4 asterisks and display the last 3 as well - JavaScript?
In JavaScript, you can mask sensitive data by replacing preceding characters with asterisks while keeping the last few characters visible. This is commonly used for phone numbers, credit cards, or account numbers. Problem Statement Given these input values: '6778922' '76633 56 1443' '8888 4532 3232 9999' We want to replace all preceding characters with 4 asterisks and display only the last 3 characters: **** 922 **** 443 **** 999 Using replace() with Regular Expression The replace() method with a regex pattern can capture the last 3 characters and replace ...
Read MoreSplitting a hyphen delimited string with negative numbers or range of numbers - JavaScript?
When working with hyphen-delimited strings containing negative numbers or ranges, simple split('-') won't work correctly because it treats every hyphen as a delimiter. We need regular expressions to distinguish between separating hyphens and negative number signs. The Problem Consider these strings with mixed content: var firstValue = "John-Smith-90-100-US"; var secondValue = "David-Miller--120-AUS"; Using split('-') would incorrectly split negative numbers and ranges, giving unwanted empty strings and broken values. Solution Using Regular Expression We use a lookahead pattern to split only at hyphens that precede letters or number ranges: var firstValue ...
Read MoreCompare the Triplets - JavaScript?
The "Compare the Triplets" problem is a popular algorithmic challenge where you compare two arrays of three integers each and count how many comparisons each array wins. Alice and Bob each have three scores, and we need to determine who wins more individual comparisons. Problem Statement Given two triplets (arrays of 3 elements each), compare corresponding elements and award points: 1 point to Alice if her element is greater, 1 point to Bob if his element is greater, 0 points for ties. Return an array with [Alice's score, Bob's score]. Example Input Alice: [5, 6, ...
Read More