OLPCities/JavaScript basic concepts: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
==Variables== |
|||
SOON |
|||
JavaScript data can be objects or not. By example: if you declare: |
|||
<pre> |
|||
x = "Peter"; |
|||
</pre> |
|||
we don't have an object. But you can declare: |
|||
<pre> |
|||
x = new String("Peter"); |
|||
</pre> |
|||
and we will have an object of the Class [[String]] having methods etc.. |
|||
The same for numbers and booleans. |
|||
If we have "not typed" data, conversions are done automatically. By example: |
|||
<pre> |
|||
x = "12"; |
|||
y = 10; |
|||
z = '7'; |
|||
And we can have: |
|||
total = x + y + z; |
|||
where "total" will be: 29. |
|||
</pre> |
|||
A "no value" is assigned with the word: <b>null</b>. |
|||
==Operators== |
|||
<pre> |
|||
+ Addiction |
|||
- Subtraction |
|||
* Multiplication |
|||
/ Division |
|||
</pre> |
|||
The operator Modulus: % is curious. If : |
|||
x = 10 % 3; |
|||
The x value is: 1. |
|||
The % operator yields the remainder of its operands from an implied division; the left operand is the dividend and the right operand is the divisor. |
|||
The possible result of a boolean operation is : true or false. The operators are: |
|||
<pre> |
|||
== is equal to |
|||
!= is not equal to |
|||
> is greater than |
|||
>= is greater than or equal |
|||
< is less than |
|||
<= is less than or equal |
|||
</pre> |
|||
We can use parenthesis to define better a operation - or its precedence. If we write: |
|||
<pre> |
|||
x = (4 >= 5); |
|||
</pre> |
|||
The value of x is: false. |
|||
An usual error is to use the assignment operator: = instead the boolean operator: == |
|||
It's usual the use of the logical operators in boolean operations: |
|||
<pre> |
|||
&& Logical AND |
|||
|| Logical OR |
|||
</pre> |
|||
What is the x value?: |
|||
x = (4>=5) && (2==2); |
|||
The answer is: false. BOTH need to be true to have a true. |
|||
And what is the y value?: |
|||
y = (4>=5) || (2==2); |
|||
The answer is: true. Only ONE needs to be true to have a true |
|||
Using the operator: // in a line of code means that nothing until the end of the line will be processed. Example: |
|||
<pre> |
|||
x = 10 % 3; //We will have this.x=1 because 10=3+3+3+1 |
|||
</pre> |
|||
We can use /* ... */ for a multi-line comment. Example: |
|||
<pre> |
|||
/* This program was created by XYZ Company |
|||
Version : 1.43 |
|||
Contact for help: John Doe - ramal 678 |
|||
*/ |
|||
</pre> |
|||
before the "real" lines of code of the program. |
|||
For concatenation of strings, for example: |
|||
<pre> |
|||
x = 'ABC' + 'DEF'; |
|||
</pre> |
|||
The x value is: ABCDEF. |
|||
It's usual, if we have a "counter", to do an operation like that: |
|||
<pre> |
|||
i = i + 1; |
|||
</pre> |
|||
Is the same to use only: |
|||
<pre> |
|||
i++; |
|||
</pre> |
|||
There are a decrement operator. So, what is this?: |
|||
<pre> |
|||
i--; |
|||
</pre> |
Revision as of 16:14, 29 September 2006
Variables
JavaScript data can be objects or not. By example: if you declare:
x = "Peter";
we don't have an object. But you can declare:
x = new String("Peter");
and we will have an object of the Class String having methods etc.. The same for numbers and booleans. If we have "not typed" data, conversions are done automatically. By example:
x = "12"; y = 10; z = '7'; And we can have: total = x + y + z; where "total" will be: 29.
A "no value" is assigned with the word: null.
Operators
+ Addiction - Subtraction * Multiplication / Division
The operator Modulus: % is curious. If :
x = 10 % 3;
The x value is: 1.
The % operator yields the remainder of its operands from an implied division; the left operand is the dividend and the right operand is the divisor.
The possible result of a boolean operation is : true or false. The operators are:
== is equal to != is not equal to > is greater than >= is greater than or equal < is less than <= is less than or equal
We can use parenthesis to define better a operation - or its precedence. If we write:
x = (4 >= 5);
The value of x is: false.
An usual error is to use the assignment operator: = instead the boolean operator: ==
It's usual the use of the logical operators in boolean operations:
&& Logical AND || Logical OR
What is the x value?: x = (4>=5) && (2==2);
The answer is: false. BOTH need to be true to have a true.
And what is the y value?:
y = (4>=5) || (2==2);
The answer is: true. Only ONE needs to be true to have a true
Using the operator: // in a line of code means that nothing until the end of the line will be processed. Example:
x = 10 % 3; //We will have this.x=1 because 10=3+3+3+1
We can use /* ... */ for a multi-line comment. Example:
/* This program was created by XYZ Company Version : 1.43 Contact for help: John Doe - ramal 678 */
before the "real" lines of code of the program.
For concatenation of strings, for example:
x = 'ABC' + 'DEF';
The x value is: ABCDEF.
It's usual, if we have a "counter", to do an operation like that:
i = i + 1;
Is the same to use only:
i++;
There are a decrement operator. So, what is this?:
i--;