Hi guys
i started jquery blog site for beginners.
The content displaying on this blogs are taken from http://jqfundamentals.com/ and other sites
Original Authors : Rebecca Murphey and others
Hi guys
i started jquery blog site for beginners.
The content displaying on this blogs are taken from http://jqfundamentals.com/ and other sites
Original Authors : Rebecca Murphey and others
In order to use flow control successfully, it’s important to understand which kinds of values are “truthy” and which kinds of values are “falsy.” Sometimes, values that seem like they should evaluate one way actually evaluate another.
Example 2.14: Values that evaluate to true
‘0’;
‘any string’;
[]; // an empty array
{}; // an empty object
1; // any non-zero number
Example 2.15: Values that evaluate to false
0;
”; // an empty string
NaN; // JavaScript’s “not-a-number” variable
null;
undefined; // be careful — undefined can be redefined!
var foo = true;
var bar = false;
if (bar) {
// this code will never run
console.log(‘hello!’);
}
if (bar) {
// this code won’t run
} else {
if (foo) {
// this code will run
} else {
// this code would run if foo and bar were both false
}
}
var foo = 1;
var bar = 0;
var baz = ‘1’;
var bim = 2;
foo == bar; // returns false
foo != bar; // returns true
foo == baz; // returns true; careful!
foo === baz; // returns false
foo !== baz; // returns true
foo === parseInt(baz); // returns true
foo > bim; // returns false
bim > baz; // returns true
foo <= baz; // returns true
Logical operators allow you to evaluate a series of operands using AND and OR operations.
Example 2.11: Logical AND and OR operators
var foo = 1;
var bar = 0;
var baz = 2;
foo || bar; // returns 1, which is true
bar || foo; // returns 1, which is true
foo && bar; // returns 0, which is false
foo && baz; // returns 2, which is true
baz && foo; // returns 1, which is true
In JavaScript, numbers and strings will occasionally behave in ways you might not expect.
Example 2.8: Addition vs. concatenation
var foo = 1;
var bar = ‘2’;
console.log(foo + bar); // 12. uh oh
Example 2.9: Forcing a string to act as a number
view sourceprint?
var foo = 1;
var bar = ‘2’;
// coerce the string to a number
console.log(foo + Number(bar));
Example 2.10: Forcing a string to act as a number (using the unary-plus operator)
console.log(foo + +bar);
Basic Operators
Basic operators allow you to manipulate values.
Example 2.5: Concatenation
var foo = ‘hello’;
var bar = ‘world’;
console.log(foo + ‘ ‘ + bar); // ‘hello world’
Example 2.6: Multiplication and division
2 * 3;
2 / 3;
Example 2.7: Incrementing and decrementing
var i = 1;
var j = ++i; // pre-increment: j equals 2; i equals 2
var k = i++; // post-increment: k equals 2; i equals 3
Syntax Basics
Understanding statements, variable naming, whitespace, and other basic JavaScript syntax.
Example 2.1: A simple variable declaration
var foo = ‘hello world’;
Example 2.2: Whitespace has no meaning outside of quotation marks
var foo = ‘hello world’;
Example 2.3: Parentheses indicate precedence
2 * 3 + 5; // returns 11; multiplication happens first
2 * (3 + 5); // returns 16; addition happens first
Example 2.4: Tabs enhance readability, but have no special meaning
var foo = function() {
console.log(‘hello’);
};
Example 1.1: An example of inline JavaScript
<script>
console.log('hello');
</script>
Example 1.2: An example of including external JavaScript
<script src='/js/jquery.js'></script>