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
Web Development Articles
Page 432 of 801
How to add a property, method to a JavaScript constructor?
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 MoreJavaScript Return an array that contains all the strings appearing in all the subarrays
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 MoreCreating JavaScript constructor using the "new" operator?
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 MoreHow to split last n digits of each value in the array with JavaScript?
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 MoreReturning values from a constructor in JavaScript?
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 MoreHow to check whether multiple values exist within a JavaScript array
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 MoreExplain the finally Statement in JavaScript with examples.
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 MoreAdd two consecutive elements from the original array and display the result in a new array with JavaScript
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 MoreWhat is JavaScript type coercion?
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 MoreJavaScript Remove all '+' from array wherein every element is preceded by a + sign
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