DEV Community

Rik de Graaff
Rik de Graaff

Posted on

How to change var to string.

Hello everyone,

I am currently studying in Javascript and wanted to know something.
My question is :

I have two input type="text" id="zzz" fields. The first ie zzz1 and the second with ID zzz2. Those are both text input fields.

Now how can I save the input (a-z) typed into the two fields?

Because when I get the input as characters, I cannot display the two variables. If I put numbers in the fields it works, but when I put alphabetical characters in the fields I get as output NaN...

How can I resolve the NaN to my first and second variable in alphabetical chars?

Thanks and Greetings,

Rik

Top comments (6)

Collapse
 
tcgumus profile image
Tuna Çağlar Gümüş

Hello Rik,

I didn't really catch the problem you are having. NaN is Not a Number error. Maybe you are trying to write string as number. Please share your code and its output so we can better understand the problem.

Also you can convert var to string with String(variable) method.

A better example could be accessed from this link: w3schools.com/jsref/tryit.asp?file...

Collapse
 
graaffrik profile image
Rik de Graaff

I've connected the two input fields with 2 variables.
Now I want to return the (text in) the variables and get the NaN output.
I think because the two var's contain only text and not numbers.

How do I get to display the var's as text?

i.imgur.com/nObbYjR.png
thepracticaldev.s3.amazonaws.com/i...

Collapse
 
graaffrik profile image
Rik de Graaff
Collapse
 
philecker profile image
Phil Ecker

If I'm understanding correctly you're trying to concatenate the value of the first input with the second?

codesandbox.io/s/boring-dawn-fbiyz

Part of your issue was you were trying to add (stackoverflow.com/questions/682626...) instead of setting your variable to the value of your input, which is why you were getting NaN. Being new to JS, I would recommend you get familiar with the Console found in Developer Tools in the browser. This can help trouble issues like the one you experienced here.

Collapse
 
graaffrik profile image
Rik de Graaff

Thanks @philecker for editing my code.
I see now what I did wrong, removed the "+"-sign before the invoerA/B.value and it works now!

I indeed wanted to concatenate the first and second input, but could even not find the error, even while using the Console under F12...
But now I get it.
Thanks again!

Collapse
 
rcarlson profile image
Robert Carlson

Can you share some of your code? Maybe send over a JSFiddle link?