Articles on Trending Technologies

Technical articles with clear explanations and examples

How to define a JavaScript function using Function() Constructor?

Nikitha N
Nikitha N
Updated on 15-Mar-2026 258 Views

The Function() constructor expects any number of string arguments. The last argument is the body of the function - it can contain arbitrary JavaScript statements, separated from each other by semicolons. Syntax new Function([arg1[, arg2[, ...argN]], ] functionBody) Parameters arg1, arg2, ...argN: Names to be used by the function as formal argument names (optional). functionBody: A string containing the JavaScript statements comprising the function definition. Example: Basic Function Creation var func = new Function("x", "y", "return ...

Read More

How to return a random number between 1 and 200 with JavaScript?

Manikanth Mani
Manikanth Mani
Updated on 15-Mar-2026 2K+ Views

To return a random number between 1 and 200, use JavaScript's Math.random() and Math.floor() methods. How It Works Math.random() generates a decimal between 0 (inclusive) and 1 (exclusive). To get integers from 1 to 200: Multiply by 200 to get 0 to 199.999... Use Math.floor() to round down to 0-199 Add 1 to shift the range to 1-200 Example You can try to run the following code to return a random number in JavaScript. ...

Read More

How to set the page-break behavior before an element with JavaScript?

Nancy Den
Nancy Den
Updated on 15-Mar-2026 619 Views

Use the pageBreakBefore property in JavaScript to set the page-break behavior before an element. Use the always property to insert a page break before an element. Note āˆ’ The changes would be visible while printing or viewing the print preview. Syntax element.style.pageBreakBefore = "value"; Property Values Value Description auto Default - automatic page break ...

Read More

Use HTML with jQuery to make a form

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 2K+ Views

To create forms with HTML and jQuery, you can either write static HTML forms or dynamically generate them using jQuery's DOM manipulation methods. Creating a Static HTML Form The basic approach uses standard HTML form elements: Student Details: Student Name: ...

Read More

How to invert an object in JavaScript?

Aman Kumar
Aman Kumar
Updated on 15-Mar-2026 3K+ Views

Inverting an object in JavaScript means swapping its keys and values, so that {name: "John", age: 25} becomes {"John": "name", "25": "age"}. This is useful for creating lookup tables or reverse mappings. There are several ways to invert objects in JavaScript, from using libraries like Underscore.js to native JavaScript methods. Using Underscore.js _.invert() The _.invert() function from Underscore.js provides a simple way to swap object keys and values. Syntax _.invert(object) Parameters: object āˆ’ The object to invert Return Value: A new object with keys and values ...

Read More

Block Scoping in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 367 Views

Block scope is an area between two curly braces { } which can be between loops, if conditions, or switch statements. The let and const keywords introduced in ES2015 allow us to create block-scoped variables that can only be accessed within that specific block. Understanding Block Scope Variables declared with let and const have block scope, meaning they exist only within the nearest enclosing block. This is different from var, which has function scope. Example: Block Scope with let Block Scoping Example ...

Read More

Setting object members in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 161 Views

In JavaScript, you can set object members (properties and methods) in several ways. This allows you to dynamically add or modify object properties after creation. Syntax // Dot notation object.propertyName = value; // Bracket notation object["propertyName"] = value; // Setting methods object.methodName = function() { // method body }; Example: Setting Object Properties Setting Object Members ...

Read More

Sorting objects according to days name JavaScript

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

Let's say, we have an array of objects that contains data about the humidity over the seven days of a week. The data, however, sits randomly in the array right now. We are supposed to sort the array of objects according to the days like data for Monday comes first, then Tuesday, Wednesday and lastly Sunday. Sample Data Following is our array: const weather = [{ day: 'Wednesday', humidity: 60 }, { day: 'Saturday', humidity: 50 }, { ...

Read More

Is there any more efficient way to code this "2 Sum" Questions JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 330 Views

Our job is to write a function that solves the two-sum problem in at most linear time. Two Sum Problem Given an array of integers, we have to find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers that add up to the target, and if no two elements add up to the target, our function should return an empty array. Brute Force Approach - O(n²) The naive approach uses nested loops to check every pair of elements: const bruteForceTwoSum ...

Read More

Get the first element of array in JavaScript

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

In JavaScript, there are several ways to get the first element of an array. The most common and efficient approaches include using bracket notation, the at() method, or destructuring assignment. Using Bracket Notation (Most Common) The simplest way is to access the element at index 0 using bracket notation: let fruits = ["apple", "banana", "orange"]; let firstFruit = fruits[0]; console.log(firstFruit); // Handle empty arrays let emptyArray = []; let firstElement = emptyArray[0]; console.log(firstElement); apple undefined Using the at() Method The at() method provides a modern alternative for accessing array ...

Read More
Showing 17721–17730 of 61,297 articles
Advertisements