Javascript Articles

Page 63 of 534

Changing ternary operator into non-ternary - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 372 Views

The ternary operator provides a concise way to write conditional expressions, but sometimes you need to convert it to regular if-else statements for better readability or debugging purposes. Understanding the Ternary Operator The ternary operator follows this syntax: condition ? valueIfTrue : valueIfFalse. It's a shorthand for simple if-else statements. Converting Ternary to If-Else Here's how to convert a ternary operator into a standard if-else statement: // Using ternary operator var number1 = 12; var number2 = 12; var result = (number1 == number2) ? "equal" : "not equal"; console.log("Ternary result:", result); // ...

Read More

How do I check that a number is float or integer - JavaScript?

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

In JavaScript, you can check if a number is a float (decimal) or integer using various methods. The most reliable approach uses the modulo operator to detect decimal values. Method 1: Using Modulo Operator The modulo operator % returns the remainder after division. For floats, value % 1 returns the decimal part: function checkNumberIfFloat(value) { return Number(value) === value && value % 1 !== 0; } var value1 = 10; var value2 = 10.15; if (checkNumberIfFloat(value1)) { console.log("The value is float=" + value1); } else { ...

Read More

Using the get in JavaScript to make getter function

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 279 Views

The get keyword in JavaScript creates a getter method that allows you to access object properties like regular properties while executing custom logic behind the scenes. Syntax const obj = { get propertyName() { // Custom logic return value; } }; Basic Example const studentDetails = { _name: "David Miller", get studentName() { ...

Read More

Fetch alternative even values from a JavaScript array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 310 Views

To fetch alternative even values from a JavaScript array, you need to iterate through the array and check if the index is even using the modulo operator. An index is even when index % 2 == 0. Syntax if (index % 2 == 0) { // This will select elements at even positions (0, 2, 4, ...) } Example The following example demonstrates how to fetch alternative even values from an array: var subjectsName = ["MySQL", "JavaScript", "MongoDB", "C", "C++", "Java"]; for (let index = 0; index ...

Read More

Object comparison Complexity in JavaScript using comparison operator or JSON.stringlify()?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

In JavaScript, comparing objects using comparison operators (== or ===) checks reference equality, not content equality. For content comparison, JSON.stringify() can be used with limitations. The Problem with Comparison Operators When comparing objects with == or ===, JavaScript checks if both variables reference the same object in memory, not if their contents are identical: var object1 = { firstName: "David" }; var object2 = { firstName: "David" }; var object3 = object1; console.log("object1 == object2:", object1 == object2); // false - different objects console.log("object1 === object2:", object1 === object2); // false - different ...

Read More

Variable defined with a reserved word const can be changed - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 168 Views

In JavaScript, variables declared with const cannot be reassigned, but this doesn't mean the contents of objects or arrays are immutable. You can still modify the properties of objects declared with const. Understanding const with Objects The const keyword prevents reassignment of the variable itself, but object properties can still be modified: const details = { firstName: 'David', subjectDetails: { subjectName: 'JavaScript' } }; // This works - modifying object properties details.firstName = 'John'; details.subjectDetails.subjectName = 'Python'; console.log(details); { firstName: 'John', subjectDetails: { ...

Read More

How do I turn a string in dot notation into a nested object with a value – JavaScript?

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

Converting a dot-notation string into a nested object is a common task in JavaScript. This approach uses split() and map() to dynamically build the nested structure. Problem Setup Let's say we have a dot-notation string representing nested property keys: const keys = "details1.details2.details3.details4.details5"; const firstName = "David"; We want to create a nested object where the final property gets the value "David". Solution Using split() and map() The technique involves splitting the dot-notation string and using map() to build nested objects: const keys = "details1.details2.details3.details4.details5"; const firstName = "David"; ...

Read More

Display the result difference while using ceil() in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 186 Views

The Math.ceil() function rounds a number UP to the nearest integer. While division might give decimal results, Math.ceil() ensures you get the next whole number. How Math.ceil() Works If your value is 4.5, 4.3, or even 4.1, Math.ceil() will return 5. It always rounds UP, unlike regular rounding. Example: Division Without ceil() Let's see normal division first: var result1 = 98; var result2 = 5; console.log("The actual result is=" + result1 / result2); The actual result is=19.6 Example: Division With ceil() Now let's apply Math.ceil() to round ...

Read More

How do I make an array with unique elements (remove duplicates) - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 258 Views

In JavaScript, there are several ways to remove duplicate elements from an array and create a new array with unique values only. Using filter() with indexOf() The filter() method combined with indexOf() is a traditional approach that keeps only the first occurrence of each element: var duplicateNumbers = [10, 20, 100, 40, 20, 10, 100, 1000]; console.log("Original array:"); console.log(duplicateNumbers); var uniqueNumbers = duplicateNumbers.filter(function (value, index, array) { return array.indexOf(value) === index; }); console.log("Array with unique elements:"); console.log(uniqueNumbers); Original array: [ 10, 20, 100, ...

Read More

How to replace some preceding characters with a constant 4 asterisks and display the last 3 as well - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 935 Views

In JavaScript, you can mask sensitive data by replacing preceding characters with asterisks while keeping the last few characters visible. This is commonly used for phone numbers, credit cards, or account numbers. Problem Statement Given these input values: '6778922' '76633 56 1443' '8888 4532 3232 9999' We want to replace all preceding characters with 4 asterisks and display only the last 3 characters: **** 922 **** 443 **** 999 Using replace() with Regular Expression The replace() method with a regex pattern can capture the last 3 characters and replace ...

Read More
Showing 621–630 of 5,340 articles
« Prev 1 61 62 63 64 65 534 Next »
Advertisements