Web Development Articles

Page 432 of 801

How to add a property, method to a JavaScript constructor?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 287 Views

In JavaScript, you can add properties and methods to constructor functions using the prototype property. This allows all instances of the constructor to share these additions. Syntax // Add a property ConstructorName.prototype.propertyName = value; // Add a method ConstructorName.prototype.methodName = function() { // method body }; Example: Adding Property and Method to Constructor Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } ...

Read More

JavaScript Return an array that contains all the strings appearing in all the subarrays

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 342 Views

We have an array of arrays like this − const arr = [ ['foo', 'bar', 'hey', 'oi'], ['foo', 'bar', 'hey'], ['foo', 'bar', 'anything'], ['bar', 'anything'] ] We are required to write a JavaScript function that takes in such array and returns an array that contains all the strings which appears in all the subarrays. Let's write the code for this function Example const arr = [ ['foo', 'bar', 'hey', 'oi'], ['foo', 'bar', 'hey'], ...

Read More

Creating JavaScript constructor using the "new" operator?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 207 Views

In JavaScript, constructor functions create objects using the new operator. A constructor function is a regular function that becomes a template for creating multiple objects with similar properties. Syntax function ConstructorName(param1, param2) { this.property1 = param1; this.property2 = param2; } // Create new object let obj = new ConstructorName(value1, value2); Example JavaScript Constructor body ...

Read More

How to split last n digits of each value in the array with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 366 Views

We have an array of mixed values like numbers and strings, and we want to extract the last n digits from each element that has enough characters. const arr = ["", 20191219, 20191220, 20191221, 20191222, 20191223, 20191224, 20191225]; We need to write a JavaScript function that takes this array and a number n. If an element contains more than or equal to n characters, the function should return only the last n characters. Otherwise, the element should remain unchanged. Solution Here's how we can implement this function using the map() method and string manipulation: ...

Read More

Returning values from a constructor in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 235 Views

In JavaScript, constructors typically don't need explicit return statements since they automatically return the newly created object. However, you can override this behavior by explicitly returning an object from a constructor. How Constructor Returns Work When you use the new keyword with a constructor: If the constructor returns an object, that object becomes the result If the constructor returns a primitive value (string, number, boolean), it's ignored and this is returned instead If there's no explicit return, this is returned automatically Example: Returning an Object from Constructor ...

Read More

How to check whether multiple values exist within a JavaScript array

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

We are required to write a JavaScript function that takes in two arrays of Numbers and checks whether all the elements of the first array exist in the second or not. Following are our arrays − const arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89]; Let's write the code and check for multiple values − Using indexOf() Method const arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89]; const contains = (first, second) => { const ...

Read More

Explain the finally Statement in JavaScript with examples.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 250 Views

The finally statement in JavaScript always executes after try and catch blocks, regardless of whether an error occurred or not. This makes it ideal for cleanup operations and code that must run in all scenarios. Syntax try { // Code that may throw an error } catch (error) { // Handle error (optional) } finally { // Always executes } Example: Finally Block Always Executes ...

Read More

Add two consecutive elements from the original array and display the result in a new array with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 384 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as the sum of two consecutive elements from the original array. For example, if the input array is: const arrayOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Then the output should be: const newArrayOne = [1, 5, 9, 13, 17] Let's write the code for this function: Example const arrayOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; ...

Read More

What is JavaScript type coercion?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 284 Views

Type coercion in JavaScript refers to the automatic conversion of values from one data type to another. This happens implicitly when JavaScript needs to perform operations between different types. How Type Coercion Works JavaScript automatically converts types in certain situations, such as when using operators or comparing values. This can sometimes lead to unexpected results if you're not familiar with the coercion rules. String Coercion Example Type Coercion Example JavaScript Type Coercion ...

Read More

JavaScript Remove all '+' from array wherein every element is preceded by a + sign

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 212 Views

When working with arrays containing strings that start with a '+' sign, you can remove these characters using JavaScript's map() method combined with replace(). Problem Statement Let's say we have an array where every element is preceded by a '+' sign: var studentNames = [ '+John Smith', '+David Miller', '+Carol Taylor', '+John Doe', '+Adam Smith' ]; Using map() with replace() The map() method creates a new array by calling a function on each element. We use replace() to ...

Read More
Showing 4311–4320 of 8,010 articles
« Prev 1 430 431 432 433 434 801 Next »
Advertisements