The document provides an overview of JavaScript syntax including variable declaration, functions, operators, loops, events, and built-in functions. It covers basic programming constructs such as if/else statements, switch statements, and regular expressions. The quick reference also lists common string methods and gives examples of opening popup windows and accessing form elements with JavaScript.
The document provides an overview of JavaScript syntax including variable declaration, functions, operators, loops, events, and built-in functions. It covers basic programming constructs such as if/else statements, switch statements, and regular expressions. The quick reference also lists common string methods and gives examples of opening popup windows and accessing form elements with JavaScript.
Var bFound= false; //Boolean //Normal Declaration Computational var iCount= 0; //Integer function sum(x,y) - Unary negation var fTotal= 2.5; //Floating Point Number { ++ Increment var sName = ""; //String return x + y; -- Decrement } * Multiplication var dNow = new Date(); //Current Date / Division var dPast= new Date(2002,5-1,20,23,59,59,999); //Optional Arguments % Modulo arithmetic // (year, month - 1, day, hours, minutes, seconds, milliseconds) function requiredString(s, bAlert) + Addition { - Subtraction var a = new Array(8); //Array var bValid= true; Logical if (s.length == 0) ! Logical NOT bValid= false; < Less than Loops Common Events > Greater than //While Loop onClick //If bAlert is not passed in, <= Less than or equal to var x = 1; onSubmit //it will be equal to "undefined“ >= Greater than or equal to while ( x <= 10 ) onMouseOver if (String(bAlert) == "true") == Equality { onMouseOut alert("String is required"); != Inequality x++; onFocus } && Logical AND } onChange || Logical OR onBlur //Dynamic Arguments ?: Conditional (unary) //For Loop onLoad function sumx() , Comma for (var x = 1; x <= 10; x++ ) onUnload { { onKeyDown var iReturn=0; Bitwise } onKeyPress ~ Bitwise NOT for (var i=0;i < arguments.length;i++) << Bitwise Shift Left //Do While Loop Built-In Functions iReturn+= arguments[i]; >> Bitwise Shift Right var i = 0; Escape >>> Unsigned Shift Right do Eval return iReturn; & Bitwise AND { isNaN } ^ Bitwise XOR document.write(i + ".<BR>"); Number | Bitwise OR i+=2; parseFloat } parseInt Branching Assignment while(i<20); String if ( (x == 1) && (y == 3) ) = Assignment Unescape sum = y - x; else Compound assignment operators sum=0; += Addition Alert, Prompt, Confirm -= Subtraction window.alert("Hello Greg!"); switch(s) *= Multiplication { /= Division sInput= prompt('Enter number to order','1'); case "A": %= Modulo Arithmetic alert("A Chosen"); <<= Left Shift if (confirm("Save before exiting?")) break; >>= Right Shift {} >>>= Unsigned Right Shift case "B": &= Bitwise And alert("B Chosen"); |= Bitwise Or Referencing Form Elements break; ^= Bitwise Xor var sFirstName= document.forms[0].txtFirstName.value; var sLastName= document.frmMain.txtLastName.value; // if all other conditions fail do this JavaScript Within Script Tag var sZip= document.forms[0].elements("txtZip").value; default; <script> alert("Some other value chosen"); function myFunction() break; {alert("Hi");} } </script> Regular Expression Pattern Test Split Into Array var pat=/^\d{5}(-{0,1}\d{4}){0,1}$/ ; var a,s; var sZip= “4322A” s="1,OH,2,IL"; if(!pat.test(sZip)) //Invalid Zip Code a= s.split(","); JavaScript in External Files (Most Efficient) {} <script defer language="Javascript" src="script/validate.js"></script>
Regular Expression Replace JavaScript Web References