JavaScript Tutorial : JavaScript Basics / PDF
JavaScript Tutorial : JavaScript Basics / PDF
Table of Contents
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Syntax Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Basic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Operations on Numbers & Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Conditional Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Truthy and Falsy Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Conditional Variable Assignment with The Ternary Operator . . . . . . . . . . . . 7
Switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
The for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
The while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
The do-while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Breaking and continuing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Reserved Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Self-Executing Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . 13
Functions as Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Testing Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
The this keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
----------------------------
Sample of the PDF document
JavaScript is a rich and expressive language in its own right. This section covers the basic concepts of
JavaScript, as well as some frequent pitfalls for people who have not used JavaScript before. While it
will be of particular value to people with no programming experience, even people who have used other
programming languages may benefit from learning about some of the peculiarities of JavaScript.
If you’re interested in learning more about the JavaScript language, I highly recommend JavaScript: The
Good Parts by Douglas Crockford.
Syntax Basics
Understanding statements, variable naming, whitespace, and other basic JavaScript syntax.
A simple variable declaration
var foo = ’ h e l l o world ’;
Whitespace has no meaning outside of quotation marks
var foo = ’ h e l l o world ’;
Parentheses indicate precedence
2 * 3 + 5; // r e t u r n s 11; m u l t i p l i c a t i o n h a p p e n s f i r s t
2 * (3 + 5); // r e t u r n s 16; a d d i t i o n h a p p e n s f i r s t
Tabs enhance readability, but have no special meaning
var foo = f u n c t i o n () {
c o n s o l e . log ( ’ hello ’);
};
Operators
Basic Operators
Basic operators allow you to manipulate values.
Concatenation
var foo = ’ hello ’;
var bar = ’ world ’;
c o n s o l e . log ( foo + ’ ’ + bar ); // ’ h e l l o world ’
Multiplication and division
2 * 3;
2 / 3;
Incrementing and decrementing
var i = 1;
var j = ++ i ; // pre - i n c r e m e n t : j e q u a l s 2; i e q u a l s 2
var k = i ++; // post - i n c r e m e n t : k e q u a l s 2; i e q u a l s 3......
JavaScript Tutorial : JavaScript Basics / PDF
0 commentaires: