Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to parse Zero (0) as integer in JavaScript

I am working on a basic calculator that takes input like this " 100 + 10/2 + 3 + 0 " and returns the output in a separate field. when I break this thing into an array zero is not parsed as integer. My code is as following

var arr = ["100", "+", "0"];
arr = arr.map(x => parseInt(x) || x);

console.log(arr);
like image 954
rudra Avatar asked Jul 21 '18 17:07

rudra


People also ask

Is zero an integer in JavaScript?

html. Passing a zero as an argument: If zero is passed to the Number. isInteger() method then it will return true as zero is also an integer.

Does parseInt remove leading zeros?

Use parseInt() to remove leading zeros from a number in JavaScript.

How do you convert to int in JavaScript?

To convert a string to an integer parseInt() function is used in javascript. parseInt() function returns Nan( not a number) when the string doesn't contain number. If a string with a number is sent then only that number will be returned as the output.

What is parse int in JavaScript?

Description. The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN . If not NaN , the return value will be the integer that is the first argument taken as a number in the specified radix .

What is the default zero value in JavaScript?

Keep in mind, the default zero value in JavaScript (and most languages) is actually the signed zero (+0). 1. Representation 2. How to create +0 and -0 in JavaScript All mathematical operations give a signed zero result (+0 or -0) that depends on the operand values.

What is the rule for negative zeros in JavaScript?

The only exception to this rule involves addition and subtraction involving +0 and -0. Any other combination of zero values gives a +0. Another thing to note is that negative zeros cannot be created as a result of addition or subtraction of non-zero operands.

How is zero represented in avascript?

J avaScript actually has two different representations for zero: positive zero, represented by +0 (or just 0 ), and negative zero, represented by -0. This is because JavaScript implements the IEEE Standard for Floating-Point Arithmetic (IEEE 754), which has signed zeroes.

How to create +0 and -0 in JavaScript?

How to create +0 and -0 in JavaScript All mathematical operations give a signed zero result (+0 or -0) that depends on the operand values. The only exception to this rule involves addition and subtraction involving +0 and -0. Any other combination of zero values gives a +0.


4 Answers

Zero is a falsy value, so short-circuiting won't work here. You need to check explicitly

var arr = ["100", "+","0"];
arr = arr.map( x => x == 0 ? 0 : (parseInt(x) || x));
console.log(arr);
like image 126
31piy Avatar answered Oct 18 '22 03:10

31piy


It's because 0 is falsy so after the parseInt("0") returns falsy you end up getting the string

Try using isNaN() instead

var arr = ["100", "+","0"];
arr = arr.map( x => isNaN(x) ? x : parseInt(x) );

// use F12 to see the console
console.log(arr); // output is being display as [100, "+","0"]
like image 44
charlietfl Avatar answered Oct 18 '22 02:10

charlietfl


Similar to other answers, zero is falsey. However, parseInt returns NaN upon failure, and that can be used to build the answer as follows:

let arr = ["100", "+", "0"];
arr = arr.map((x) => {
  const parsed = parseInt(x);
  return Number.isNaN(parsed) ? x : parsed;
});
console.log(arr);

IMO this is a better solution as it is explicit about the return types that parseInt returns, and doesn't rely on type coercion.

Note: There are fun nuances with isNaN. The ES2015 Number.isNaN is used here to prevent any issues with type coercion, although it's not strictly necessary in this case.

like image 44
AnilRedshift Avatar answered Oct 18 '22 04:10

AnilRedshift


parseInt("0") is falsy. You can use Number() to convert 0 to an integer.

Try the following:

var arr = ["100", "+", "0"];
arr = arr.map(x => !isNaN(Number(x))? Number(x) : x);

console.log(arr);
like image 1
amrender singh Avatar answered Oct 18 '22 02:10

amrender singh