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 405 of 534
What is non-enumerable property in JavaScript and how can it be created?
Objects can have properties that don't show up when iterated through the particular object using Object.keys() or for...in loop. Those type of properties are called as non-enumerable properties. What are Non-enumerable Properties? Non-enumerable properties are object properties that are hidden from enumeration methods like Object.keys(), for...in loops, and Object.entries(). However, they can still be accessed directly using their property name. Creating Non-enumerable Properties To create a non-enumerable property we have to use Object.defineProperty() method. This is a special method to create non-enumerable properties in an object. Syntax Object.defineProperty(object, propertyName, { ...
Read MoreDescribe pass by value and pass by reference in JavaScript?
JavaScript passes data to functions in two ways: pass by value for primitives (numbers, strings, booleans) and pass by reference for objects and arrays. Understanding this distinction is crucial for avoiding unexpected behavior in your code. Pass by Value In pass by value, a function receives a copy of the variable's value. Changing this copy inside the function doesn't affect the original variable. JavaScript primitives (numbers, strings, booleans, null, undefined) are always passed by value. Example let a = 1; let change = (val) ...
Read MoreWhat is function chaining in JavaScript?
Function chaining is a technique that allows you to call multiple methods on the same object in sequence using dot notation. This makes code more concise and improves readability by eliminating the need to repeatedly reference the same object. How Function Chaining Works For function chaining to work, each method must return the object itself (typically using return this). This allows the next method in the chain to be called on the returned object. Without Function Chaining In this example, the methods don't return this, so chaining is not possible: ...
Read MoreHow to get a part of string after a specified character in JavaScript?
To get a part of a string after a specified character in JavaScript, you can use the substring() method combined with indexOf(). This technique allows you to extract portions of text before or after any character. Understanding substring() The substring() method extracts characters from a string between two specified indices. It takes a start index (inclusive) and an optional end index (exclusive). Syntax for Getting Text After a Character string.substring(string.indexOf(character) + 1); Here, indexOf(character) finds the position of the character, and adding + 1 starts extraction from the next position. Syntax for ...
Read MoreWhat is the main difference between objects created using object literal and constructor function?
The main difference between objects created using object literal and constructor function lies in how they handle references and instances. Objects created with object literals are referenced by variables, while constructor functions create independent instances. Let's explore both approaches with examples to understand this fundamental difference. Objects Created Using Object Literal When you create an object using object literal syntax and assign it to multiple variables, all variables point to the same object in memory. This means any change made through one variable affects all other variables referencing that object. Example ...
Read MoreWhat is global namespace pollution in JavaScript?
Global namespace pollution occurs when too many variables and functions are declared in the global scope, leading to name collisions. This is especially problematic in large projects using multiple JavaScript libraries. What is Name Collision? Name collision happens when two or more scripts define variables or functions with the same name in the global scope. The last definition overwrites previous ones, causing unexpected behavior. Example: Two Teams, Same Function Name Let's demonstrate with two JavaScript files from different teams: TeamA1.js Team A1 creates a student constructor with 2 parameters: function student(fname, lname) ...
Read MoreHow to remove html tags from a string in JavaScript?
Removing HTML tags from strings is a common task in JavaScript web development. You can accomplish this using regular expressions to match and replace HTML tag patterns with empty strings. Understanding HTML Tag Structure HTML elements are enclosed between angle brackets like , , , etc. By targeting the pattern of content within these brackets, we can effectively strip all HTML tags from a string. Syntax str.replace(/(]+)>)/ig, ''); The regular expression /(]+)>)/ig breaks down as: ]+) - matches one or more characters that aren't closing brackets > - matches closing angle bracket ...
Read MoreHow to count a number of words in given string in JavaScript?
Counting words in a JavaScript string requires handling various whitespace scenarios like multiple spaces, leading/trailing spaces, and newlines. Using regular expressions provides an effective solution. The Challenge Strings can contain irregular spacing that affects word counting: Leading and trailing spaces Multiple consecutive spaces between words Newlines with spaces Empty strings Step-by-Step Approach Step 1: Remove Leading and Trailing Spaces Use regex to eliminate spaces at the start and end of the string: str.replace(/(^\s*)|(\s*$)/gi, ""); Step 2: Reduce Multiple Spaces to Single Space Replace consecutive spaces with a ...
Read MoreHow to access a function property as a method in JavaScript?
In JavaScript, object properties can hold functions, which become methods when accessed through the object. A method is simply a function that belongs to an object and can access the object's other properties using the this keyword. Syntax const object = { property1: value1, property2: value2, methodName: function() { return this.property1 + this.property2; } }; // Call the method object.methodName(); Example 1: Employee Object with fullName Method Here's an ...
Read MoreHow to hide e-mail address from an unauthorized user in JavaScript?
Hiding email addresses from unauthorized users helps protect privacy and reduce spam. JavaScript provides several methods to obfuscate email addresses while keeping them readable for legitimate users. Method 1: Partial Masking with Asterisks This approach replaces part of the username with asterisks, keeping the domain visible: function hideEmail(email) { var parts = email.split("@"); var username = parts[0]; var domain = parts[1]; if (username.length 2 ? ...
Read More