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
Object Oriented Programming Articles
Page 2 of 589
How to find the biggest number in an array around undefined elements? - JavaScript
We are required to write a JavaScript function that takes in an array that contains some numbers, some strings and some falsy values. Our function should return the biggest Number from the array. For example − If the input array is the following with some undefined values − const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; Then the output should be 65 Solution Approach We'll filter out non-numeric values and find the maximum among valid numbers. The key is to properly identify numeric values while handling ...
Read MoreBehavior of + operator in JavaScript to store large numbers?
JavaScript's + operator converts strings to numbers, but regular numbers have precision limits. For large integers beyond JavaScript's safe range, use BigInt() to avoid precision loss. The Problem with + Operator for Large Numbers JavaScript numbers use 64-bit floating point, which can safely represent integers up to Number.MAX_SAFE_INTEGER (2^53 - 1). Beyond this limit, the + operator causes precision loss: console.log("JavaScript's safe integer limit:"); console.log(Number.MAX_SAFE_INTEGER); // Small number - works fine var stringValue1 = "100"; console.log("Small number with + operator:"); console.log(+stringValue1); // Large number - precision loss var stringValue2 = "2312123211345545367"; console.log("Large number with ...
Read MoreLooping numbers with object values and push output to an array - JavaScript?
In JavaScript, you can create an array from an object by mapping object values to specific positions using Array.from(). This technique is useful when you have sparse data that needs to be converted into a dense array format. Problem Overview Given an object with numeric keys and values, we want to create an array where the object values are placed at positions corresponding to their keys, filling missing positions with a default value. var numberObject = { 2: 90, 6: 98 }; console.log("The original object:"); console.log(numberObject); The original object: { '2': 90, '6': ...
Read MoreJavaScript: lexical scoping issue using new keyword constructor while adding inner function?
When using constructor functions with inner functions, a common lexical scoping issue arises where the inner function cannot access the constructor's this context. This happens because inner functions have their own execution context. The Problem Inner functions don't inherit the this binding from their parent constructor, leading to undefined or incorrect values when trying to access constructor properties. function Employee() { this.technologyName = "JavaScript"; function workingTechnology() { // 'this' here refers to global object, not ...
Read MoreHow to new line string - JavaScript?
In JavaScript, there are several ways to create new lines in strings depending on where the output will be displayed. For HTML content, use tags, while for console output or plain text, use the escape sequence . Using Tag for HTML Display When displaying text in HTML elements, use the tag to create line breaks: New Line in HTML Original Text ...
Read MoreChanging ternary operator into non-ternary - JavaScript?
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 MoreHow do I check that a number is float or integer - JavaScript?
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 MoreUsing the get in JavaScript to make getter function
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 MoreFetch alternative even values from a JavaScript array?
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 MoreObject comparison Complexity in JavaScript using comparison operator or JSON.stringlify()?
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