Javascript Articles

Page 322 of 534

Strange syntax, what does `?.` mean in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

The ?. operator is called optional chaining and provides a safe way to access nested object properties without throwing errors when intermediate properties are undefined or null. The Problem with Regular Property Access Consider this nested object describing a person: const being = { human: { male: { age: 23 } } }; console.log(being.human.male.age); 23 This works fine, but what happens if we try to access a property that doesn't exist? ...

Read More

How -Infinity is converted to String in JavaScript?

Sravani Alamanda
Sravani Alamanda
Updated on 15-Mar-2026 374 Views

In JavaScript, -Infinity is a special numeric value that represents negative infinity. When we need to convert it to a string, we can use the String() method or the toString() method. The -Infinity value typically results from mathematical operations like dividing a negative number by zero or when a calculation exceeds the minimum representable number in JavaScript. Syntax The String() method converts any JavaScript value to a string representation: String(value) Where value is any JavaScript value. The method returns the string representation of that value. Using String() Method The most reliable ...

Read More

What is this problem in JavaScript's selfexecuting anonymous function?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 222 Views

Let's analyze this JavaScript code snippet that demonstrates a common confusion with variable hoisting and function declarations in self-executing anonymous functions. var name = 'Zakir'; (() => { name = 'Rahul'; return; console.log(name); function name(){ let lastName = 'Singh'; } })(); console.log(name); Naive Analysis (Incorrect) At first glance, you might expect this sequence: Global variable name is set to 'Zakir' Inside the IIFE, name is ...

Read More

What is the role of shiftKey Mouse Event in JavaScript?

Rishi Rathor
Rishi Rathor
Updated on 15-Mar-2026 283 Views

The shiftKey property is a boolean property of mouse events that indicates whether the Shift key was pressed when the mouse event occurred. This property is commonly used to modify the behavior of mouse interactions based on keyboard modifiers. Syntax event.shiftKey Return Value The shiftKey property returns: true - if the Shift key was pressed during the mouse event false - if the Shift key was not pressed during the mouse event Example: Basic shiftKey Detection Here's how to detect if the Shift key ...

Read More

How to print star pattern in JavaScript in a very simple manner?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Here is a simple star pattern that we are required to print inside the JavaScript console. Note that it has to be printed inside the console and not in the output or HTML window: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Here's the code for doing so in JavaScript: Example const star = "* "; // where length is no of stars in longest streak const length = 6; for(let i = 1; i

Read More

How to set the shape of the border of the top-right corner with JavaScript?

Fendadis John
Fendadis John
Updated on 15-Mar-2026 272 Views

To set the shape of the border of the top-right corner in JavaScript, use the borderTopRightRadius property. This property allows you to create rounded corners by specifying the radius value. Syntax element.style.borderTopRightRadius = "value"; The value can be specified in pixels (px), percentages (%), or other CSS units like em or rem. Example Here's how to dynamically change the top-right border radius using JavaScript: #box { ...

Read More

Title Case A Sentence JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 330 Views

Let's say we are required to write a function that accepts a string and capitalizes the first letter of every word in that string and changes the case of all the remaining letters to lowercase. For example, if the input string is — hello world coding is very interesting The output should be — Hello World Coding Is Very Interesting Let's define a function capitaliseTitle() that takes in a string and capitalises the first letter of each word and returns the string — Using Split and Map Method ...

Read More

How [ ] is converted to Boolean in JavaScript?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 15-Mar-2026 218 Views

In JavaScript, an empty array [] is considered a truthy value when converted to Boolean. This might seem counterintuitive, but it follows JavaScript's type coercion rules for objects. Converting [] to Boolean Use the Boolean() method to explicitly convert an empty array to Boolean: Convert [] to Boolean var myVal = []; document.write("Boolean: " + Boolean(myVal)); ...

Read More

When summing values from 2 arrays how can I cap the value in the new JavaScript array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 165 Views

When working with arrays that represent RGB color values, you often need to add corresponding elements while ensuring the result doesn't exceed the maximum value of 255. This is a common requirement in graphics programming and color manipulation. Suppose we have two arrays, each containing three elements representing the red, green, and blue color values as integers. Our goal is to add the corresponding values to form a new RGB color array, capping any value that exceeds 255. Problem Statement We need to create a function that: Takes two arrays as input (representing RGB values) Adds ...

Read More

What will happen when { } is converted to String in JavaScript?

Arushi
Arushi
Updated on 15-Mar-2026 185 Views

In JavaScript, when an empty object {} is converted to a string, it becomes "[object Object]". This happens because JavaScript calls the object's toString() method during string conversion. How Object to String Conversion Works JavaScript follows these steps when converting an object to a string: First, it calls the object's toString() method For plain objects, toString() returns "[object Object]" This is the default string representation for all plain objects Example: Converting Empty Object to String Convert {} to String ...

Read More
Showing 3211–3220 of 5,340 articles
« Prev 1 320 321 322 323 324 534 Next »
Advertisements