# Can anyone explain parseInt without using the word "parse"?

Hi all,
Maybe I’m thinking about it too hard but here is the conversation between me and parseInt():

Q: How does this method work?
A: (FCC) The parseInt function converts its first argument to a string, parses it, and returns an integer or NaN.
Q: Ok what does “parse” mean?
A: (Dictionary) Verb: To analyze (a sentence) into its parts and describe their syntactic roles. (or in computing) an act of or the result obtained by parsing a string or a text.

Next question: What are electrolytes and why do they use them to make Brawndo. I guess I need to forget about the “number” object and the “Integer” argument and focus more on the string? yeah, that has to be it right? its not a math function so much as a concatenating of strings…Your silence often brings me to the answer…seriously, when I can’t wrap my head around something and stare at it over and over, its the framing of the question that helps me the most. If anyone DOES know of a good resource I’d love to know more. I’ve seen all the obvious ones (fcc and the links it provides, w3schools). I think the string idea above is the right direction though.
Feedback?
PS I literally made a meme about it right before posting this HAHA
Cheers

`parseInt()` takes the argument, converts it to a string, and then converts it to an integer.

Parse is a technical term in the process. Its good to see technical terms in context and get accustomed to them.

1 Like

A Javascript object you can use is: `Number("3")`
It’s like doing `parseInt("3")` but a lot more obvious.

It also works with floating point numbers: `Number("3.5")`
Does the same as `parseFloat("3.5")` but again, more obvious.

You can see parseInt and parseFloat are methods of this object. But as they write: " Values of other types can be converted to numbers using the `Number()` function ."

2 Likes

Thanks DM. I had been reading it as taking a number and “parsing” it but seeing it take a “number” like “10” which is not a “number” or a `Number` (object) was helpful.

``````Number.parseInt("10");
// expected return is 10
``````

I like that you mention “technical…context”. That is a more elegant way I could have framed the question. The definitions and descriptions I have seen don’t really break it down. The irony is that “parse” is often defined as to “break down” and “analyze” but all three terms literally mean the same thing ( analysis , “a breaking-up” or “an untying;” from ana- and lysis “a loosening”).

I still don’t really understand how to use it consistently. If you stumble on any technical examples that are easy for a noob like me to grasp, I would appreciate them. I am going to just play with different string args and read up on the Number object. Also, Ijust re-watched the “Brawndo has what plants crave” scene from Idiocracy. Its brilliant.

I would use it as follows

``````let a = 4.5
let b = "3"

let aNum = Number(a) // 4.5
let bNum = Number(b) // 3

let aInt = Number.parseInt(a) // 4
let bInt = Number.parseInt(b) // 3
``````

I think

1 Like

Right, the example given does not do the same thing.

• The `Number` function attempts to forcibly convert the argument it’s given to a number regardless of what type of thing the argument is (it coerces a given type of thing to a number).
• `parseInt` parses a string in an attempt to find the first numbers in it, which it then coerces to a number.

It’s important to understand common terms.

“Parsing” has specific meaning in the context of computing, just looking up the dictionary definition + synonyms isn’t going to tell you much. It has a wikipedia page for example. A Google should give you many, many pages with the computer science and the linguistics definitions of parsing (which are not exactly the same, but very similar).

Yes it is a form of analysis, but domain-specific terms of language (“jargon”) are important, because they allow practitioners in that domain to better understand what each other mean.

In this context, it means that the thing doing the parsing (the program or function or person doing it on paper or whatever) takes a string of characters, breaks them up into seperate characters or groups of characters and tags them according to some arbitrary set of rules. So for this function:

1. `parseInt` takes a string, reads the string forwards until it reaches a number character.
2. If there are some characters before the first number character, they make up imthe first group.
3. Then it reads forward until it reaches a character that is not a number.
4. The character before that are all numbers, and they will make up the second group.
5. As it only needs to find the first number, the rest of the string can be ignored (let’s call that the third group).
6. As it only looks for numbers, the first group can be ignored.

So as an illustration of how it works:

``````parseInt("hello123hello")
↑    ↑   ↑
g1   g2  g3
// discard g1, ignore g3, so now left with:
parseInt("123")
// attempt conversion to a number, success:
123

parseInt("hello")
↑
g1
parseInt("")
// attempt conversion to a number, fail:
NaN

parseInt("123.45")
↑  ↑
g1 g2
// "." is the first non-number character after
// g1, so ignore that and everything after (g2)
parseInt("123")
// attempt conversion to a number, success:
123

parseInt(123.45)
// parseInt takes a string as an argument.
// JS tries to be helpful and converts the
// number to a string:
parseInt("123.45")
// Now it works exactly the same as the previous example
``````
4 Likes

You can just use Number without chaining parseInt to it. Try it in the console: Number(“123”) will return 123 as a number.

Number(“hello123”) returns NaN (not a number).

Number.parseInt(“hello123”) also returns NaN.

parseInt(“hello123”) returns NaN.

parseInt(“123”) returns 123

1 Like

Not sure if you already got your answer on this, but the word “parse” has these synonyms: https://www.merriam-webster.com/thesaurus/parse

“Parse” is often used in a technical sense to refer to any code that takes an input and returns something. It has a different meaning than “functions” in that “parsing” usually has the subtext of scanning or analyzing its input, while “functions” have a broader meaning.

In the plainest English possible, parseInt takes an input string that looks like a number (but isn’t a number, as strings and numbers are different and aren’t necessarily interchangeable) and actually converts it to a number.

parseInt is often used when getting input from forms when reading in numbers that were originally entered as strings. Or to ensure validity that a string contains a number.

2 Likes

Those make sense to me.
`Number('0x11') // 17`
makes less sense but I get that some letters are used as numbers as in hexadecimal codes like `#ff0000`. I remember some people used `parseInt` to convert roman numerals. That made sense. 1016 has a 6, and a 10, and a 1000 to deal with all separately. I looked at a Euler #11 solution someone did where they used parseInt to avoid writing out all 20 arrays (20X20 two digit numbers in a grid).
Euler11 parseInt example

paeseInt() is a JS function that return / extract number from a string.

1 Like

Right on. The first two synonyms: “dis-sect” and “ana-lyze” are both compounds that describe splitting into parts and performing some kind of sorting process or triage. I hope you all don’t think I reached for the google dictionary as my first call. Just another data point. I’m actually fascinated that so many of these words have the same roots…it speaks to the difficulty in getting the marrow in these explanations. I have been going back to finish up my front end design project the last few weeks and I think my JS brain is a little rusty.

Dan Couper mentioned above the “arbitrary set of rules” its uses to parse the input. Well said

So here’s a thing. When you type in an input, even of type `number`, the value of that input is a string. In order to use that value as a number rather than a string, we need a way to strip any “string-y” bits away and simply have a number.

So `"34"` would become the number `34` after running thru parseInt(), while `"3 blind mice"` would become `3`.

Also, `parseFloat()` would work similarly, though the number would convert to a mixed (decimal) value - `"3.5 men"` would become `3.5` with `parseFloat()`, but would become `3` with `parseInt()` (the decimal bit is truncated, leaving the integer).

Thanks. In other words it cleans the data. My problem initially was that the radix part wasn’t making sense to me. I think I panic a little and over-focus when I don’t understand part of the problem.

1 Like

Easy to do. I’ll often pull things like this into a sandbox (https://codepen.io, https://repl.it for example) and break it in various ways. They’re great for exactly this, building a test case and hammering on it in the various examples given on MDN, then building my own.

Great question, shows you’re not taking anything for granted.

2 Likes

Thanks, its a balancing act. Sometimes you have to shut out the questions and let them sort themselves out as you go. Like when you watch a movie and people keep on saying “HEY who is THAT guy?!?”. You gotta be ok with not knowing everything. I take comfort at this point in my life knowing there is a REASON it doesn’t work. There is a lot of abstraction in coding. I could have learned a lot of things more quickly were I not asking so many questions. They hang me up!

1 Like

how familiar are you with different numerical systems? to convert different ones to decimal (our usual way of counting) you can use pareInt and an appropriate radix

1 Like

I’m aware of the idea. Humans use base 10. computers use binary.
`console.log(roughScale(' 0xF', 16)); //expected output: 1500`
and
`parseInt(021, 8)// returns 15`
were not making a lot of sense.

1 Like

if you have a number in binary (ex 100) and you want to convert it to decimal, you can write `parseInt(100, 2)` and it will output 4

I get that 100 is a string and 2 is the radix, but I wouldn’t be able to tell you the output would be 4

that’s why you have parseInt to make the conversion. the radix tells `parseInt` the numeral system in which the number is written