Is var a = 5 equivalent to var a = "5" ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Is var a = 5 equivalent to var a = "5" ?

Note it's related to JavaScript

2nd Jun 2017, 3:36 PM
_Retr0/-
_Retr0/- - avatar
14 Answers
+ 4
var a = 5; assigns the integer value 5 to the variable a. While, var a = "5" assigns the string value of 5 to the variable a. So, one is an int and one is a string. Javascript still has strings, even though you assign everything to a var.
2nd Jun 2017, 3:40 PM
Rrestoring faith
Rrestoring faith - avatar
+ 6
Also you can simplify those if statements alot from lines 63-172. You could probably do something along the lines of this: if(counter == 0) num1 = x; else if(counter == 1) num2 = x; else if(counter == 2) num3 = x; else if(counter == 3) num4 = x; Then: if(counter < 4) counter++; Instead of 100+ lines of code 😝. If you want to simplify it even FURTHER, the nums can be an array. Then you can do something along the lines of: if(counter < 4) nums[counter++] = x; To get the same result with 2 lines of code. 2 lines is easier to debug than over 100. So, I suggest trying this; although you will need to change some other things to work with the array.
2nd Jun 2017, 6:06 PM
Rrestoring faith
Rrestoring faith - avatar
+ 4
The value of y is always NaN. You initialized it when num1+num2+.. are null. You can always do things like: alert(y); to do debugging. You also have 2 variables called y 😝 If you wrote y = ""+num1+num2+num3+num4; In the check function, before comparing, this could fix your problems. However, you don't even really use variables num1, num2 etc.. In the function where the user presses a button you create new vars var num1 = "1"; etc.. So, the global variables num1, num2 etc.. are still null. So, y = ""+num1+... will still be undefined.
2nd Jun 2017, 6:02 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
It's not really as simple as yes or no with this question. var a = 5; var b = '5'; document.write(a==b); /*true*/ document.write('<br>'); document.write(a===b); /*false*/ a==b JavaScript will see the values here as equal so it will return true. a===b here we also do a type comparison, and since one is a Number and the other is a String it will return false.
2nd Jun 2017, 5:41 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
https://code.sololearn.com/WTss4tksLhuC/?ref=app can you explain why am I not winning even after inputting the right answer
2nd Jun 2017, 5:47 PM
_Retr0/-
_Retr0/- - avatar
+ 2
You can parse (convert) things from integers to strings and vise versa. https://www.w3schools.com/jsref/jsref_parseint.asp https://www.w3schools.com/jsref/jsref_string.asp
2nd Jun 2017, 3:51 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
Yes
2nd Jun 2017, 4:06 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
thanks well that's lot of mistakes😀😀 I will rectify them
2nd Jun 2017, 6:05 PM
_Retr0/-
_Retr0/- - avatar
+ 1
oh very important info thx
2nd Jun 2017, 5:41 PM
_Retr0/-
_Retr0/- - avatar
0
alright thx , but is their a way I can assign the same variable to string "5" and integer 5
2nd Jun 2017, 3:48 PM
_Retr0/-
_Retr0/- - avatar
0
can I use the number keyword?
2nd Jun 2017, 3:56 PM
_Retr0/-
_Retr0/- - avatar
0
Woah lol 2 lines man that's so compact I will do it once I complete this project
3rd Jun 2017, 6:06 AM
_Retr0/-
_Retr0/- - avatar
0
I checked it and it with you debugging method of alerting y it says undefined 4 times
3rd Jun 2017, 8:01 AM
_Retr0/-
_Retr0/- - avatar
0
I just recorded an important observation if you set the variable nums outside the assign function as (whichever no. u want) it updates that no. and not the no. set in the function
3rd Jun 2017, 8:06 AM
_Retr0/-
_Retr0/- - avatar