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 407 of 534
How 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 find maximum value in an array using spread operator in JavaScript?
In this article, we will learn how to find the maximum value in an array using the spread operator with Math.max() in JavaScript. This approach is much cleaner and more efficient than passing array elements individually. What is the Spread Operator? The spread operator (...) expands an array or object into individual elements. It's perfect for passing array elements as separate arguments to functions like Math.max(). // Syntax Math.max(...arrayName) The Problem with Manual Approach Without the spread operator, you'd need to pass each array element individually to Math.max(), which becomes tedious for large ...
Read MoreHow to use spread operator to join two or more arrays in JavaScript?
The spread operator (...) provides a clean, modern way to join multiple arrays in JavaScript. It can be used for both immutable merging (creating new arrays) and mutable merging (modifying existing arrays). What is the Spread Operator? The spread operator (...) expands array elements, allowing you to copy values from one array to another. It performs a shallow copy of the original array. const mergedArray = [...array1, ...array2]; Method 1: Immutable Array Joining This approach creates a new array containing elements from all source arrays, leaving the original arrays unchanged. Joining Two ...
Read MoreIs 'floating-point arithmetic' 100% accurate in JavaScript?
In JavaScript, floating-point arithmetic is not 100% accurate due to how computers store decimal numbers in binary format. This leads to precision errors that can affect calculations involving decimal values. Understanding Floating Point Numbers A floating-point number is any number with a decimal point, such as 1.52, 0.14, or -98.345. In JavaScript, all numbers are stored using the IEEE 754 double precision format, which uses 64 bits to represent numbers. Sign Exponent (11 bits) Mantissa/Fraction (52 bits) ...
Read MoreHow to select a radio button by default in JavaScript?
Radio buttons allow users to select one option from multiple choices. Unlike checkboxes, only one radio button in a group can be selected at a time. To make a radio button selected by default, you can use the HTML checked attribute or JavaScript's checked property. Basic Radio Button Syntax Radio buttons are created using the HTML tag with type="radio": Option 1 Option 2 Method 1: Using HTML checked Attribute The simplest way to select a radio button by default is adding the checked attribute directly in HTML: ...
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 MoreHow to clone an array using spread operator in JavaScript?
In this article, we are going to discuss how to use the spread operator to clone an array in JavaScript. Cloning is the process of copying one array into another array. Previously, the slice() method was used to clone an array, however, ES6 now provides the spread operator (...) to simply clone an array. What is Array Cloning? An Array is a data structure in JavaScript that can hold multiple values at once. Cloning involves copying an array's elements to create a new independent array. ...
Read MoreWhat is the importance of _isEqual() method in JavaScript?
The _.isEqual() method from Underscore.js and Lodash libraries provides deep equality comparison for JavaScript objects. Unlike native JavaScript comparison operators, it performs value-based comparison rather than reference-based comparison. Why _.isEqual() is Important JavaScript's native equality operators (== and ===) only check if two objects are the same reference in memory, not if they have the same content. The _.isEqual() method solves this by performing deep comparison of object properties, regardless of property order. Syntax _.isEqual(object1, object2); It accepts two values as parameters and returns true if they are equivalent, false otherwise. Example: ...
Read More