var=alpahabet or var=numerics? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

var=alpahabet or var=numerics?

in javascript i saw that var x=100 which shows output as 100 and in some cases var x=y var y=100 document.write(x); which shows output as 100 what if var 100=x?

5th Jan 2019, 10:44 AM
Venkata kalyan chakravarthi sanagapalli
Venkata kalyan chakravarthi sanagapalli - avatar
7 Answers
+ 8
Venkata kalyan chakravarthi sanagapalli Var is used to declare variables in JavaScript. There are some rules in every language when naming a variable. The answer by Rishi Anand clearly states the 5 points to note while naming a variable. Based on that answer let's see few valid and invalid examples. Valid names: myVar, _myVar, $myVar, myVar33, my_var InValid names: 3myVar (shouldnt start with a no.) my Var (no space), var (reserved keyword), my#Var (# not accepted character) 100 ( should nt start with a numberl)
5th Jan 2019, 3:36 PM
Morpheus
Morpheus - avatar
+ 7
David Carroll it was a mistake on my end 😅, due to wrongly copying the code here. Y was declared so hoisting would come into play. I have corrected my answer and the example code now.
5th Jan 2019, 2:59 PM
Morpheus
Morpheus - avatar
+ 6
Venkata kalyan chakravarthi sanagapalli But for me it's undefined, am I missing something? var x = y; var y = 100; document.write(x) ; // undefined Anyway, I took above code as an opportunity to go into Hoisting concept in JavaScript for var. Because of which we get undefined above. https://code.sololearn.com/Wb2wF7oT3n6n/?ref=app
5th Jan 2019, 1:47 PM
Morpheus
Morpheus - avatar
+ 5
Morpheus I'm not sure if the question has been updated since your response. It does show that y is declared with var keyword before y is referenced. As the question is currently worded, hoisting would apply.
5th Jan 2019, 2:45 PM
David Carroll
David Carroll - avatar
+ 2
var 100=x; will give you syntax error = is an assignment operator and as per the rule value to the right of = is assigned to the variable to the left of =. In simple terms L.H.S of = should be a variable and R.HS. of = should be value. There are few rules to be followed while naming a variable : i)it can consits of letters,digits,_,and $ ii)it must not begins with digit iii)it has to be a single world iv)keywords are not allowed to be used as variables v)uppercase and lowercase are different see by yourself whether you variable name is following all the above 5 rules or not
5th Jan 2019, 10:57 AM
Rishi Anand
Rishi Anand - avatar
+ 1
Variable is nothing but a symbolic name given to a memory location. The property of the variable is that it changes during the execution of program You are right...var keyword is used to define a variable in JavaScript. I have already mentioned the rules for naming a variable. In your case 100 can't be used as variable because it begins with a digit. And as I already mentioned in point ii, variable can't begins with digit 100 is an invalid name for a variable _100 is a valid name $100 is a valid name
5th Jan 2019, 3:36 PM
Rishi Anand
Rishi Anand - avatar
0
I don't understand what you said guys I just want to know for what var is used. And if it is used to define a variable then why a number can't be variable and why alphabets are considered as variable?
5th Jan 2019, 3:21 PM
Venkata kalyan chakravarthi sanagapalli
Venkata kalyan chakravarthi sanagapalli - avatar