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
Javascript Articles
Page 406 of 534
How to remove non-word characters in JavaScript?
To remove non-word characters in JavaScript, we use regular expressions to replace unwanted characters with empty strings. Non-word characters include symbols, punctuation, and special characters that aren't letters, numbers, or underscores. What are Non-Word Characters? Non-word characters are anything that doesn't match the \w pattern in regex. This includes symbols like !@#$%^&*(), punctuation, and special characters, but excludes letters (a-z, A-Z), digits (0-9), and underscores (_). Using Simple Regex Pattern The most straightforward approach uses the \W pattern, which matches any non-word character: function removeNonWordChars(str) { if ...
Read MoreHow to decode an encoded string in JavaScript?
In JavaScript, string decoding refers to converting encoded strings back to their original form. While escape() and unescape() were historically used, modern JavaScript provides better alternatives like decodeURIComponent() and decodeURI(). Using unescape() (Deprecated) The unescape() method decodes strings encoded by the escape() method. It replaces hexadecimal escape sequences with their corresponding characters. Syntax unescape(string) Example // Special character encoded with escape function var str = escape("Tutorialspoint!!"); document.write("Encoded : " + str); document.write(""); ...
Read MoreHow to parse an URL in JavaScript?
JavaScript provides several methods to parse URLs and extract their components like protocol, hostname, pathname, and query parameters. The modern approach uses the built-in URL constructor, while legacy methods involve creating DOM anchor elements. Using the URL Constructor (Modern Approach) The URL constructor is the standard way to parse URLs in modern JavaScript. It creates a URL object with all components easily accessible. const url = new URL("https://www.youtube.com/watch?v=tNJJSrfKYwQ"); console.log("Protocol:", url.protocol); console.log("Host:", url.host); ...
Read MoreHow to add two strings with a space in first string in JavaScript?
To concatenate two strings in JavaScript, we use the '+' operator. When the first string already contains a trailing space, we can directly concatenate without adding an explicit space. However, if there's no space, we need to add one manually between the strings. Method 1: First String Has Trailing Space When the first string already ends with a space, simple concatenation is sufficient: function concatenateStrings(str1, str2) { return (str1 ...
Read MoreWhat is the use of Object.is() method in JavaScript?
The Object.is() method determines whether two values are strictly equal, providing more precise comparison than the regular equality operators. Syntax Object.is(value1, value2) Parameters value1: The first value to compare value2: The second value to compare Return Value Returns true if both values are the same, false otherwise. How Object.is() Determines Equality Two values are considered the same when they meet these criteria: Both are undefined or both are null Both are true or both ...
Read MoreHow many ways can a property of a JavaScript object be accessed?
JavaScript object properties can be accessed in two main ways: dot notation and bracket notation. Each method has its own use cases and advantages. Dot Notation The dot notation is the most common and readable way to access object properties when the property name is known at compile time. object.propertyName Bracket Notation The bracket notation uses square brackets with the property name as a string. This method is more flexible and allows dynamic property access. object["propertyName"] object[variableName] Example: Using Dot Notation var person = ...
Read MoreHow to check whether an array is a true array in JavaScript?
In JavaScript, arrays are actually objects, which makes type checking tricky. When you use the typeof operator on an array, it returns "object" rather than "array", making it unreliable for array detection. The Problem with typeof The typeof operator cannot distinguish between arrays and plain objects since both return "object". Syntax typeof operand Parameters: The typeof operator takes an operand and returns a string indicating the data type of the operand. Example: typeof with Arrays and Objects var a = [1, 2, 5, ...
Read MoreHow to access nested json objects in JavaScript?
Accessing nested JSON objects in JavaScript involves navigating through multiple levels of object properties. Nested objects are objects contained within other objects, creating a hierarchical data structure. What are Nested JSON Objects? A nested JSON object has one or more objects as property values. This creates multiple layers that you need to traverse to access specific data. Using Dot Notation The most common way to access nested properties is using dot notation, where you chain property names with dots. Example 1: Single Level Nesting var person = ...
Read MoreHow to modify properties of a nested object in JavaScript?
There are two methods to modify properties of nested objects in JavaScript. One is Dot notation and the other is Bracket notation. The functionality is the same for both methods, but they differ in their syntax and use cases. Let's discuss them in detail with practical examples. Dot Notation Method Dot notation is the most common way to access and modify nested object properties. Use this when property names are valid identifiers and known at compile time. Example In the following example, initially the value of property country is England. Using dot notation, the value ...
Read MoreHow to find duplicates in an array using set() and filter() methods in JavaScript?
Finding duplicates in JavaScript arrays is a common task that can be accomplished using modern JavaScript methods. The Set() constructor and filter() method provide elegant solutions for removing duplicates without complex logic. Using Set() Method The Set() constructor automatically stores only unique values, making duplicate removal straightforward. When combined with the spread operator, it creates a new array with duplicates removed. Syntax let uniqueArray = [...new Set(originalArray)]; Example var dupNames = ['John', 'Ram', 'Rahim', 'Remo', 'Ram', 'Rahim']; var uniArr = [...new ...
Read More