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
Object Oriented Programming Articles
Page 81 of 589
How to Title Case a sentence in JavaScript?
Title case formatting converts the first letter of each word in a sentence to uppercase while keeping the remaining letters lowercase. This is commonly used for headings, titles, and proper formatting of text. Algorithm Split the sentence into individual words using string.split() method Convert all letters in each word to lowercase using string.toLowerCase() method Loop through each word and capitalize the first letter using toUpperCase() Concatenate the capitalized first letter with the remaining lowercase letters Join all words ...
Read MoreWrite a palindrome program in JavaScript so that only alphanumeric values should be allowed?
A palindrome is a string that reads the same forwards and backwards. When checking palindromes with alphanumeric characters only, we need to ignore spaces, punctuation, and special characters, considering only letters (a-z) and digits (0-9). For example, "A man, a plan, a canal: Panama" becomes "amanaplanacanalpanama" after removing non-alphanumeric characters, which is indeed a palindrome. Basic Palindrome Check Here's a simple approach using string manipulation methods to check if a string is a palindrome: var str = "racecar"; ...
Read MoreHow to prevent modification of object in JavaScript ?.
JavaScript provides three methods to prevent modification of objects at different levels of restriction. These protective measures ensure that objects cannot be accidentally or intentionally altered, maintaining code integrity and predictable behavior. Three Levels of Object Protection 1) Prevent Extensions Object.preventExtensions() prevents new properties from being added to an object, but existing properties can still be modified or deleted. Example var object1 = { prop1: 1 }; Object.preventExtensions(object1); delete ...
Read MoreWhat 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 MoreHow to add a number and a string in JavaScript?
In JavaScript, when you use the + operator with a number and a string, JavaScript performs concatenation instead of mathematical addition. This is because the presence of a string changes the operation from addition to string concatenation. How It Works JavaScript follows these rules when using the + operator: Number + Number: Mathematical addition String + Number: String concatenation (number is converted to string) Number + String: String concatenation (number is converted to string) Example var a = 5 + 5; ...
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 More