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 452 of 801
JavaScript Check for case insensitive values?
JavaScript provides several methods to check for case insensitive values. The most common approaches use toLowerCase(), toUpperCase(), or regular expressions. Using toLowerCase() Method Convert both values to lowercase before comparison: let name1 = "JOHN"; let name2 = "john"; console.log(name1.toLowerCase() === name2.toLowerCase()); // true console.log("Hello".toLowerCase() === "HELLO".toLowerCase()); // true true true Using Regular Expression Use the i flag for case insensitive matching: let allNames = ['john', 'John', 'JOHN']; let makeRegularExpression = new RegExp(allNames.join("|"), "i"); let hasValue = makeRegularExpression.test("JOHN"); console.log("Is Present=" + hasValue); // Direct regex approach let ...
Read MoreHow to de-structure an imported object in JavaScript?
Destructuring allows you to extract specific properties from imported objects in JavaScript modules. This is particularly useful when you only need certain properties from a larger object. Basic Destructuring Syntax When importing an object, you can destructure it immediately or after import: // Method 1: Destructure after import import person from "./sample.js"; let {firstName, lastName, age} = person; // Method 2: Direct destructuring (named exports) import {firstName, lastName, age} from "./sample.js"; Example: Complete Implementation sample.js (Module file) export default { firstName: 'Rohan', ...
Read MoreSplit Space Delimited String and Trim Extra Commas and Spaces in JavaScript?
When working with strings that contain multiple commas and spaces, you can use regular expressions with split() and join() methods to clean them up effectively. The Problem Consider this messy string with multiple consecutive commas and spaces: var sentence = "My, , , , , , , Name, , , , is John ,, , Smith"; console.log("Original string:", sentence); Original string: My, , , , , , , Name, , , , is John ,, , Smith Solution: Using Regular Expression with split() and join() The split(/[\s, ]+/) method splits ...
Read MoreHow to assign static methods to a class in JavaScript?
To assign static methods to a class in JavaScript, prefix the method with the static keyword. Static methods belong to the class itself rather than instances and can be called without creating an object. Syntax class ClassName { static methodName() { // method body } } // Call static method ClassName.methodName(); Basic Static Method Example class Calculator { static add(a, b) { return a ...
Read MoreSetting property in an empty object using for loop in JavaScript.
In JavaScript, you can populate an empty object with properties using various loop methods. The most common approaches are the for...in loop and the for...of loop with Object.entries(). Using for...in Loop The for...in loop iterates over all enumerable properties of an object, making it ideal for copying properties from one object to another. Setting Properties with for...in Loop Setting Properties in Empty Object Populate ...
Read MoreSingle dimensional array vs multidimensional array in JavaScript.
JavaScript supports both single-dimensional and multidimensional arrays. A single-dimensional array stores elements in a linear sequence, while a multidimensional array contains arrays as elements, creating a matrix-like structure. Single-Dimensional Arrays A single-dimensional array is a simple list of elements accessed by a single index. Single Dimensional Array let singleArray = [10, 20, 30, 40, 50]; console.log("Single-dimensional array:", ...
Read MoreHow to draw a circle in JavaScript?
Drawing circles in JavaScript is accomplished using the HTML5 Canvas API and the arc() method. This method allows you to create perfect circles and arcs with precise control over position, size, and styling. Basic Circle Drawing Syntax context.arc(x, y, radius, startAngle, endAngle, counterclockwise); Parameters Parameter Description x, y Center coordinates of the circle radius Circle radius in pixels startAngle Starting angle (0 for full circle) endAngle Ending angle (2 * Math.PI for full circle) Interactive Circle Example ...
Read MoreHow to split string when the value changes in JavaScript?
To split a string when the character value changes in JavaScript, you can use the match() method with a regular expression that captures consecutive identical characters. Syntax string.match(/(.)\1*/g) How the Regular Expression Works The pattern /(.)\1*/g breaks down as: (.) - Captures any single character \1 - Matches the same character as captured in group 1 * - Matches zero or more of the preceding element g - Global flag to find all matches Example var originalString = "JJJJOHHHHNNNSSSMMMIIITTTTHHH"; var regularExpression = /(.)\1*/g; console.log("The original string = ...
Read MoreHow to create a unique ID for each object in JavaScript?
In JavaScript, creating unique IDs for objects is essential for tracking and identifying instances. The most reliable method is using Symbol(), which generates guaranteed unique identifiers. Using Symbol() for Unique IDs The Symbol() function creates a unique symbol every time it's called, even with the same description. This makes it perfect for generating unique object identifiers. Unique Object IDs body { ...
Read MoreHow to access an object through another object in JavaScript?
In JavaScript, you can access properties and methods of one object from another object by referencing them directly. This technique is useful for sharing data between objects or creating relationships. Basic Property Access The simplest way is to reference properties directly from another object: Object Access Example CLICK HERE // First object with properties and method let obj = { firstName: "Rohan", lastName: "Sharma", welcome() { ...
Read More