How to add a property to a JavaScript object using a variable as the name?

AmitDiwan
Updated on 15-Mar-2026 23:19:00

396 Views

In JavaScript, you can add properties to objects using either dot notation or bracket notation. However, when you need to use a variable as the property name, bracket notation is the only viable approach. The Problem with Dot Notation Dot notation treats everything after the dot as a literal property name, not a variable: let obj = {a: "value1"}; let propertyName = "dynamicProp"; // This creates a property literally named "propertyName" obj.propertyName = "value2"; console.log(obj); console.log("Property 'dynamicProp' exists:", obj.dynamicProp); // undefined { a: 'value1', propertyName: 'value2' } Property 'dynamicProp' exists: undefined ... Read More

JavaScript subtraction of two float values?

Yaswanth Varma
Updated on 15-Mar-2026 23:19:00

3K+ Views

JavaScript floating-point arithmetic can produce unexpected results due to precision limitations. To correctly subtract two float values and get accurate results, use parseFloat() to ensure proper number conversion and toFixed() to control decimal precision. The Problem with Float Subtraction Direct subtraction of floats can yield imprecise results: var result1 = 0.3 - 0.1; var result2 = 0.2 - 0.1; document.write("0.3 - 0.1 = " + result1 + ""); ... Read More

Create a to-do list with JavaScript

Yaswanth Varma
Updated on 15-Mar-2026 23:19:00

2K+ Views

A TODO list is a simple application that helps users keep track of tasks they need to complete. It typically allows users to add new tasks, mark them as completed, edit existing tasks, and delete unnecessary ones. In this tutorial, we'll learn how to create a functional to-do list using JavaScript. Our to-do list will support the following core features: Add new tasks Delete tasks Mark tasks as completed Edit existing tasks Basic To-Do List Implementation Let's start with a simple example ... Read More

How to form a two-dimensional array with given width (columns) and height (rows) in JavaScript ?

AmitDiwan
Updated on 15-Mar-2026 23:19:00

469 Views

We are required to write a JavaScript function that takes in three arguments: height --> no. of rows of the array width --> no. of columns of the array val --> initial value of each element of the array Then the function should return the new array formed based on these criteria. Method 1: Using Array.apply() with map() This approach creates arrays using Array.apply() and fills them with map(): const rows = 4, cols = 5, val = 'Example'; const fillArray = (width, height, value) => { ... Read More

Fibonacci like sequence in JavaScript

Nikitasha Shrivastava
Updated on 15-Mar-2026 23:19:00

494 Views

In JavaScript, you can generate a Fibonacci-like sequence using various approaches including loops and recursion. The Fibonacci sequence is fundamental in programming and demonstrates different algorithmic techniques. What is the Fibonacci Sequence? The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding numbers. The sequence starts with 0 and 1, then continues as: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Formula: F(n) = F(n-1) + F(n-2), where F(0) = 0 and F(1) = 1 Method 1: Generate Fibonacci Up to a Certain Number ... Read More

Finding average of n top marks of each student in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

678 Views

Suppose, we have an array of objects that contains information about some students and the marks scored by them over a period of time like this: const marks = [ { id: 231, score: 34 }, { id: 233, score: 37 }, { id: 231, score: 31 }, { id: 233, score: 39 }, { id: 231, score: 44 }, { id: 233, score: 41 }, { id: 231, score: ... Read More

Finding longest consecutive joins in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

223 Views

We are required to write a JavaScript function that takes in an array of pairs of numbers, arr, as the first and the only argument. In every pair, the first number is always smaller than the second number. Now, we define a pair (c, d) that can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion. Our function is supposed to find the length longest chain which can be formed. Problem Example For example, if the input to the function is: const arr ... Read More

List the important core components of React Native

Shilpa S
Updated on 15-Mar-2026 23:19:00

539 Views

React Native provides core components that map to native platform views. These components are essential building blocks for creating cross-platform mobile applications. Core Components Overview React Native Component Android Native View iOS Native View Web Browser Description ... Read More

How to call a JavaScript function in HTML?

Mayank Agarwal
Updated on 15-Mar-2026 23:19:00

2K+ Views

In this article, we will explore how to call and initialize JavaScript functions from HTML templates. JavaScript functions allow us to execute desired operations and respond to user interactions on web pages. There are several ways to call JavaScript functions in HTML. We'll discuss the most common approaches with practical examples. Using Event Handlers (onclick) The most common way to call JavaScript functions is through event handlers like onclick. When a user interacts with HTML elements (buttons, links, etc.), the associated JavaScript function executes. JavaScript Function Call Example ... Read More

How to set the radius of a Circle using FabricJS?

Rahul Gurung
Updated on 15-Mar-2026 23:19:00

422 Views

In this tutorial, we are going to learn how to set the radius of a Circle using FabricJS. We can specify the position, colour, opacity and dimension of a circle object in the canvas, but first we will have to specify a radius for our circle to show up. This can be done by using the radius property. Syntax new fabric.Circle({ radius: Number }: Object) Parameters options (optional) − This parameter is an Object which provides additional customizations to our circle. Using this ... Read More

Advertisements