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 83 of 589
Can re-declaring a variable destroy the value of that variable in JavaScript?
Re-declaring a variable will not destroy the value of that variable, until and unless it is assigned with some other new value. If we look at the following example variables "x" and "y" were assigned with values 4 and 8 respectively, later on when those variables were reassigned, the old values were replaced with the new values and displayed as shown in the output. Example: Re-declaring with New Values var x = new Number(4); ...
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 MoreHow to hide/show HTML elements in JavaScript?
JavaScript provides several ways to hide and show HTML elements by manipulating their CSS properties. The most common approach is using the display property with values like none (to hide) and block (to show). Hiding an Element To hide an element, set its display property to none. This removes the element from the document flow completely. Example In the following example, clicking the "Hide Me" button hides the paragraph text: Using JavaScript to hide HTML elements. Showing an Element ...
Read MoreHow to pass arrays as function arguments in JavaScript?
In JavaScript, you can pass arrays as function arguments using two main approaches: the traditional apply() method and the modern ES6 spread operator (...). The spread operator is now the preferred approach as it provides cleaner, more readable code. Using apply() Method (Traditional) The apply() method was the traditional way to pass array elements as individual arguments to a function. It requires two parameters: the context (this value) and the array of arguments. Syntax functionName.apply(thisContext, arrayOfArguments) Example function displayMarkets(market1, market2, market3) { ...
Read MoreHow to get the length of an object in JavaScript?
In JavaScript, objects don't have a built-in length property like arrays and strings. When you try to access object.length, it returns undefined. To get the number of properties in an object, you need to use specific methods. Why Objects Don't Have a Length Property The length property only works for arrays and strings, not plain objects: var object = {prop1: 1, prop2: 2}; document.write("Object length: " + object.length); Object length: undefined Arrays and Strings Have Length For comparison, ...
Read MoreWhat is the importance of "enumerable" attribute in defining a property in JavaScript object?
The enumerable attribute controls whether a property appears in object enumeration methods like for...in loops, Object.keys(), and JSON.stringify(). When using Object.defineProperty(), properties are non-enumerable by default. Syntax Object.defineProperty(objectName, propertyName, { value: propertyValue, enumerable: true/false }) Default Behavior: Non-enumerable Property When enumerable is not specified, it defaults to false, making the property invisible to enumeration methods: var object = {one: 1}; Object.defineProperty( object, ...
Read More