What is the difference between parseInt(string) and Number(string) in JavaScript?

V Jyothi
Updated on 15-Mar-2026 23:18:59

354 Views

In JavaScript, parseInt() and Number() both convert strings to numbers, but they handle invalid characters differently. Understanding their behavior is crucial for proper string-to-number conversion. parseInt() Method The parseInt() method parses a string character by character and stops at the first non-digit character, returning the parsed integer portion. console.log(parseInt("765world")); console.log(parseInt("50px")); console.log(parseInt("123.45")); console.log(parseInt("abc123")); 765 50 123 NaN Number() Method Number() attempts to convert the entire string to a number. If any part of the string is invalid, it returns NaN. console.log(Number("765world")); console.log(Number("50px")); console.log(Number("123.45")); console.log(Number("123")); NaN NaN ... Read More

Usage of background-color property in CSS

Sravani S
Updated on 15-Mar-2026 23:18:59

168 Views

The background-color property is used to set the background color of an element. It accepts color values in various formats including color names, hexadecimal, RGB, and HSL values. Syntax background-color: color | transparent | inherit | initial; Example: Using Color Names You can use predefined color names to set the background color: Background Color Example This text ... Read More

How can we validate decimal numbers in JavaScript?

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

685 Views

Validating decimal numbers in JavaScript is essential for form validation and data processing. There are several reliable methods to check if a value is a valid decimal number. Method 1: Using Regular Expression Regular expressions provide precise control over decimal number validation patterns: Decimal Validation Decimal Number Validation Validate ... Read More

Compress array to group consecutive elements JavaScript

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

463 Views

We are given a string that contains some repeating words separated by dash (-) like this: const str = 'monday-sunday-tuesday-tuesday-sunday-sunday-monday-monday-monday'; console.log(str); monday-sunday-tuesday-tuesday-sunday-sunday-monday-monday-monday Our job is to write a function that returns an array of objects, where each object contains two properties: val (the word) and count (their consecutive appearance count). Expected Output Format For the above string, the compressed array should look like this: const arr = [{ val: 'monday', count: 1 }, { val: 'sunday', ... Read More

How to group JSON data in JavaScript?

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

3K+ Views

Grouping JSON data in JavaScript involves organizing objects by a common property. This is useful when you need to categorize data or create summary reports from complex datasets. Basic Grouping with for...in Loop The traditional approach uses a for...in loop to iterate through object keys and group items by a specific property: var details = { "1": { name: "John" }, "2": { name: "John" ... Read More

How to use the 'with' keyword in JavaScript?

Nitya Raut
Updated on 15-Mar-2026 23:18:59

6K+ Views

The with keyword in JavaScript creates a shorthand for referencing an object's properties and methods. However, it's deprecated and not recommended in modern JavaScript due to performance and security issues. Syntax with (object) { // properties used without the object name and dot } Basic Example Here's how with works with a simple object: JavaScript with Statement var person = { ... Read More

Set the Background Image Position with CSS

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

659 Views

To set the background image position, use the background-position property. This CSS property controls where the background image is positioned within its container element. Syntax background-position: horizontal vertical; The property accepts various values including pixels, percentages, and keywords like left, right, center, top, and bottom. Example: Positioning with Pixels This example sets the background image position 80 pixels from the left and centers it vertically: body { ... Read More

How to access 'this' keyword inside an arrow function in JavaScript?

vineeth.mariserla
Updated on 15-Mar-2026 23:18:59

661 Views

The JavaScript 'this' keyword refers to the object it belongs to. Arrow functions handle 'this' differently than regular functions - they inherit 'this' from their surrounding scope rather than creating their own. How Arrow Functions Handle 'this' Arrow functions don't have their own 'this' binding. Instead, they capture the 'this' value from the enclosing lexical scope at the time they are defined. function Student(fname, grade) { this.fname = fname; this.grade = grade; this.details = function() { ... Read More

What is "undefined x 1" in JavaScript?

Ayush Gupta
Updated on 15-Mar-2026 23:18:59

259 Views

The "undefined x 1" notation is not a JavaScript feature but Chrome's way of displaying sparse arrays with uninitialized elements. Instead of showing every undefined value, Chrome uses compact notation for better readability. What Creates "undefined x n" When you create an array with the Array() constructor or have gaps in array indexes, Chrome displays uninitialized slots as "undefined x count": console.log(Array(5)); [undefined × 5] Sparse Arrays Example Arrays with missing indexes also show this notation: let arr = [1, , , 4]; // Missing elements at ... Read More

JavaScript regex program to display name to be only numbers, letters and underscore.

Alshifa Hasnain
Updated on 15-Mar-2026 23:18:59

780 Views

In this article, we will learn the regex program to validate names to contain only numbers, letters, and underscores in JavaScript. Validating user input is essential for maintaining data integrity and security in web applications. JavaScript Regex for Name Validation Regular expressions (regex) are powerful tools for defining patterns to search and validate text. A common validation requirement is ensuring a name consists only of letters (A-Z, a-z), digits (0-9), and underscore (_). To ensure that a name contains only allowed characters, we use the following regex pattern: /^\w+$/ Regex Pattern Breakdown ... Read More

Advertisements