Home  • Programming • JavaScript

JavaScript variable and declaration

To declare variable in javaScript you can use var keyword. However the var keyword is optional in javascript. Example:
<script>
var x;
y=6
x=20;

document.write(x+y);
</script>

Output

26

Comments 6


semi colon ";" is also optional in javascrpt
hi this out putput want to see in a text box without using button. Is it possible?
You have to work with DOM (Document Object Model) then
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <script type="text/javascript"> function calThumbs down{ switchThumbs down{ case 1: document.form1.display.value =1; break; case 2: document.form1.display.value =2; break; case 3: document.form1.display.value =3; break; case 4: document.form1.display.value =4; break; case 5: document.form1.display.value =5; break; case 6: document.form1.display.value =6; break; case 7: document.form1.display.value =7; break; case 8: document.form1.display.value =8; break; case 9: document.form1.display.value =9; break; case 0: document.form1.display.value =0; break; } } var num=0; var i=0; function oparator(op) { var n=parseFloat(document.form1.display.value); if(i==0) { num=n; document.form1.display.value=""; i ; } else { switch(op) { case ' ': num =n;document.form1.display.value="";break; case '-': num-=n;document.form1.display.value="";break; case '*': num*=n;document.form1.display.value="";break; case '/': num =n;document.form1.display.value="";break; case '%': num%=n;document.form1.display.value="";break; case '=': document.form1.display.value=num; num=0;break; } } } </script> </head> <body> <pre> <form id="form1" name="form1" method="post" action=""> <input name="display" type="text" size="20" /> <input type="button" value="1" onclick="cal(1)"/> <input type="button" value="2" onclick="cal(2)"/> <input name="button" type="button" value="3" onclick="cal(3)"/> <input type="button" value="4" onclick="cal(4)"/> <input type="button" value="5" onclick="cal(5)"/> <input type="button" value="6" onclick="calDevil"/> <input name="button" type="button" value="7" onclick="cal(7)"/> <input type="button" value="8" onclick="calNote"/> <input type="button" value="9" onclick="cal(9)"/> <input type="button" value="0" onclick="cal(0)"/> <input type="button" value=" " onclick="oparator(' 'Wink"/> <input type="button" value="-" onclick="oparator('-'Wink"/> <input type="button" value="*" onclick="oparator('*'Wink"/> <input type="button" value="/" onclick="oparator('/'Wink"/> <input type="button" value="%" onclick="oparator('%'Wink"/> <input type="button" value="=" onclick="oparator(''Wink"/> </form> </pre> </body> </html>
how create calculation function
Copyright © 2024. Powered by Intellect Software Ltd