-1

Any whole number ending in a dot returns the number in javascript console.(except decimal numbers)

like > 1. returns 1. Adding >1+1. also works. I don't understand why

typeof(1) // 'number'
typeof(1.) //'number'

However, when I put the same number inside a function, regex test gives a wrong output.

i.e,

const regex = /^\d+$/ //checks if there is a number inside a string
regex.test('1') // true
regex.test(1)   //true
regex.test('1.') // false

The workaround I have is simply regex.test(Number('1.'))

Fatah
  • 2,184
  • 4
  • 18
  • 39
  • 1
    `\d` is not a Number, it's a Digit and has nothing to do with js types – k102 Jun 21 '18 at 08:51
  • `Number('1.')` > `1`, that is why `^\d+$` matches it. If you want to match numbers like `10.`, `1.1` and `10`, use `/^\d+\.?\d*$/`. To match also `.0`, use `/^(?!\.$)\d*\.?\d*$/` or (to disallow empty string) `/^(?!\.?$)\d*\.?\d*$/`. – Wiktor Stribiżew Jun 21 '18 at 08:52
  • `\d` represents only a digit which not same as decimal –  Jun 21 '18 at 08:53
  • Try `/^(\d|.)+$/` – Emil S. Jørgensen Jun 21 '18 at 08:53
  • Since you just asked about the explanation of the behavior, it seems a dupe of [this SO thread](https://stackoverflow.com/questions/11798903/javascript-casts-floating-point-numbers-to-integers-without-cause). Closed as such. – Wiktor Stribiżew Jun 21 '18 at 09:00
  • @WiktorStribiżew I had an answer ready to post with I suddenly couldn't. ... and as that dupe doesn't explain the _regex_ part, please open it again so I can add my answer (can't reopen single-handed...yet :) – Asons Jun 21 '18 at 09:13
  • @LGSon Do you want to explain how `/^\d+$/` works? No need to, [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) is enough. And [regex101.com](https://regex101.com/r/EQf1RX/1) will do the rest. – Wiktor Stribiżew Jun 21 '18 at 09:19
  • 1
    @WiktorStribiżew I want to say all that + `typeof` (summerized) in a much simpler way, like this: ....... With your sample using `typeof` you ask if a value is of a certain type, and since a dot `.` is a valid part of a number, being a decimal separator, it will return `number` _In below sample I added a few to show what happens_ --- When it comes to your _regex_, and using `\d` to test a value, you are saying, _Is this string digits_, which then will fail when it contains a dot `.`, and that is because a dot is not a digit. – Asons Jun 21 '18 at 09:21
  • I think the added dupe reasons will cover all the topics now. These misunderstandings are too basic (matching digits, dots, float/int numbers and the `Number` casting) and has been thoroughly explained. And BTW, I had plenty of the same situations when someone closed a question and I could not post my answer, it is just how SO works. – Wiktor Stribiżew Jun 21 '18 at 09:25
  • 1
    @WiktorStribiżew Since the dupe doesn't explain anything about the _regex_ part in this question, I disagree completely with it being a dupe. ... and it has actually nothing to do with the me weren't able to post. – Asons Jun 21 '18 at 09:28
  • Thanks for the clear explanation @WiktorStribiżew. – Fatah Jun 21 '18 at 09:28
  • 1
    @fatahn Well, it was actually me who provided the above clear explanation – Asons Jun 21 '18 at 09:29
  • @LGSon All regex explanation requests are closed as dupes of "What does this regex mean". The link is added. – Wiktor Stribiżew Jun 21 '18 at 09:29
  • oops sorry didnt see that. Thanks a lot @LGSon :))) – Fatah Jun 21 '18 at 09:30
  • I think your answer is what I needed. Should I rephrase this question? @LGSon – Fatah Jun 21 '18 at 09:31
  • @WiktorStribiżew Now it make more sense with the updated dupe links...still, a clean, easy-to-understand explanation would be of great value to be attached to this question, with which you obviously don't agree. – Asons Jun 21 '18 at 09:32
  • 1
    @fatahn No, your question works as is, it is a summarized answer that is missing, and I can't add that anymore, as it were closed. – Asons Jun 21 '18 at 09:33
  • 1
    @WiktorStribiżew And btw, no where in those links is it explicit stated, in a simple, so beginners can easily understand it, way...which is why I am somewhat persistent in having such answer posted. Bottom line, you loose nothing, beginners get a nice, less technical answer.... and I have reopened some of my closed questions for less than that, and am not alone thinking like that: https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean#comment73918657_22944075 – Asons Jun 21 '18 at 09:51

1 Answers1

0

JavaScript has a single type for all numbers: it treats all of them as floating-point numbers. However, the dot is not displayed if there are no digits after the decimal point:

5.000 = 5

Also, \d matches a digit, not a number.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Fabien Greard
  • 1,854
  • 1
  • 16
  • 26
  • `it treats all of them as floating-point numbers` Most JS Engines are a bit more clever than that.. :) – Keith Jun 21 '18 at 08:59
  • That is true but engine vs javascript developer point of view are not the same. – Fabien Greard Jun 21 '18 at 09:03
  • `\d`matches a digit, not a number - does that affect `test`? a test for digits looks for presence of a numerical value. Also `'5.' == 5` returns true. I don't understand because I expect `'5.'` to be different from `5.` – Fatah Jun 21 '18 at 09:06
  • It's not the same, This is because you used lenient equality, which cast '5.' To a number , try with a strict one. `===` – Fabien Greard Jun 21 '18 at 09:11