Javascript

Functions and operators

Functions
A function can be used as standalone code to make it easier to execute multiple lines of Javascript code, e.g. in response to <a href="javascript:my_func();">Execute</a> In Javascript 1.2, functions can also be assigned to a Javascript object to create a method for that object. Use the keyword this to refer to the object for which the function is a method.

<script type="text/javascript">
<!--
function sum(x,y) {
return x + y;
}
var total = sum(4,7);
alert(total);
-->
</script>

Operators

Javascript Operators
OperatorOperation:Syntax
=assignmentx="one";
[]Access an array element array[<name|number>]
() Invoke a function or separate operations myfunc(<values>) x=(y*z)-4;
++ pre or post increment by one++x or x++
-- pre or post decrement by one--x or x--
+ -addition, minusx-y+z
+=,-=,*= shorthand for assign and add etc. a+=x; same as: a=a+x;
! boolean false if (!var) {}
deletedelete an object property delete object;
newcreate a new object new object;
typeofreturn type of operand typeof var;
voidreturn undefined value var = void;
* / % multiply, divide, modulusc=((x*y)/z)%a;
+string concatenation x="one"+" two";
<,<= less than, less than or equalif(x<="2"){}
>,>= greater than, greather than or equalif(x>="2"){}
==, != test for equality or inequalityif(x=="1") {}
&& Logical ANDif((x!="2")&&(y=="1")) {}
|| Logical ORif((x!="2")||(y=="1")) {}
? :Conditional, if ?then:else if(x=="2") ? {y=2;} : {y=3;}
. Access object propertyobject.property

When using multiple operators, use () to make sure the calculation works as you expect.
x=y-2*z; will actually execute as x=y-(2*z);. To calculate x as y minus 2 all multiplied by z, use x=(y-2)*z;



This is part of www.codehelp.co.uk Copyright © 1998-2004 Neil Williams
See the file about.html for copying conditions.