Check whether Enter key is pressed or not and display the result in console with JavaScript?

AmitDiwan
Updated on 15-Mar-2026 23:18:59

398 Views

In JavaScript, you can detect when the Enter key is pressed using the onkeypress event handler. This is useful for form submissions, search functionality, or triggering actions when users press Enter. Key Code for Enter Key The Enter key has a key code of 13. We can check this using the keyCode property of the event object. Basic Implementation First, create an input field with an onkeypress event handler: Then, create a function to detect the Enter key: function checkEnterKey(event) { if (event.keyCode == 13) ... Read More

Shift certain array elements to front of array - JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:18:59

289 Views

We are required to write a JavaScript function that takes in an array of numbers. The function should bring all the 3-digit integers to the front of the array. Let's say the following is our array of numbers: const numList = [1, 324, 34, 3434, 304, 2929, 23, 444]; Understanding 3-Digit Numbers A 3-digit number is any integer between 100 and 999 (inclusive). We can check this using a simple condition: const isThreeDigit = num => num > 99 && num < 1000; console.log(isThreeDigit(324)); // true console.log(isThreeDigit(34)); // ... Read More

How to get the tangent of a number in JavaScript?

Shubham Vora
Updated on 15-Mar-2026 23:18:59

585 Views

In this tutorial, we will learn how to get the tangent of a number in JavaScript. The tangent of an angle is defined by the ratio of the length of the opposite side to the length of the adjacent side. It can also be defined as the ratio of sine and cosine function of an acute angle where the value of cosine function should not be zero. Mathematically: tan θ = opposite side / adjacent side also, tan θ = sin θ / cos θ Where θ = angle in radians The tangent of a ... Read More

What are different Navigator methods available?

Jennifer Nicholas
Updated on 15-Mar-2026 23:18:59

303 Views

The Navigator object provides several methods to interact with the browser and detect client capabilities. Here are the main Navigator methods available in JavaScript: Navigator Methods Overview Method Description Browser Support ... Read More

AVL Rotations in Javascript

Sai Subramanyam
Updated on 15-Mar-2026 23:18:59

515 Views

To balance itself, an AVL tree may perform the following four kinds of rotations: Left rotation Right rotation Left-Right rotation Right-Left rotation The first two rotations are single rotations and the next two rotations are double rotations. To have an unbalanced tree, we at least need a tree of height 2. With this simple tree, let's understand them one by one. Left Rotation If a tree becomes unbalanced when a node is inserted into the right subtree of ... Read More

Replace a value if null or undefined in JavaScript?

AmitDiwan
Updated on 15-Mar-2026 23:18:59

1K+ Views

In JavaScript, you can replace null or undefined values using the logical OR operator (||) or the nullish coalescing operator (??). Syntax // Using logical OR operator var result = value || defaultValue; // Using nullish coalescing operator (ES2020+) var result = value ?? defaultValue; Using Logical OR Operator (||) The || operator returns the first truthy value or the last value if all are falsy: var value = null; console.log("The original value:", value); var actualValue = value || "This is the Correct Value"; console.log("The replaced value:", actualValue); // ... Read More

Square every digit of a number - JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:18:59

511 Views

We are required to write a JavaScript function that takes in a number and returns a new number in which all the digits of the original number are squared and concatenated. For example: If the number is − 9119 Then the output should be − 811181 because 9² is 81 and 1² is 1. Example Following is the code − const num = 9119; const squared = num => { const numStr = String(num); let res = ''; ... Read More

How to compare two JavaScript Date Objects?

Ramu Prasad
Updated on 15-Mar-2026 23:18:59

439 Views

JavaScript Date objects can be compared directly using comparison operators (>, =, date2) { document.write("Current date is more recent."); } else { document.write("Custom date is more recent."); } Current date: Mon May 28 2018 09:48:49 GMT+0530 (India Standard Time) Custom date: Thu Dec 10 2015 20:15:10 GMT+0530 (India Standard Time) Current date is more recent. Comparing for ... Read More

What is the role of clearTimeout() function in JavaScript?

Srinivas Gorla
Updated on 15-Mar-2026 23:18:59

370 Views

The clearTimeout() function cancels a timeout that was previously set with setTimeout(). When you call setTimeout(), it returns a timeout ID that you can use with clearTimeout() to stop the scheduled execution. Syntax clearTimeout(timeoutID) Parameters timeoutID - The identifier returned by setTimeout() that you want to cancel Basic Example clearTimeout Example Start Timer Cancel Timer ... Read More

What will happen when [50,100] is converted to Number in JavaScript?

Sai Subramanyam
Updated on 15-Mar-2026 23:18:59

185 Views

Use the Number() method in JavaScript to convert to Number. When an array like [50, 100] is converted to Number in JavaScript, the result might be surprising. What Happens with Array to Number Conversion? When Number() is called on an array, JavaScript first converts the array to a string, then attempts to convert that string to a number. For arrays with multiple elements, this results in NaN (Not a Number). Example Convert [50, 100] to Number ... Read More

Advertisements