Created by Brian Duffey
![]() |
- 10 minutes | ![]() |
- 1 day | ![]() |
- 10-15 minutes |
![]() |
|||||
![]() |
- 2 days | ![]() |
- 1 day | ![]() |
- 10 minutes |
x = 3;
function myFunc() {
x = 3;
y = 4;
// More code below here that uses x and y
}
Def.: the set of variables, objects, and functions you have access to |
|
|
|
|
|
|
|
|
|
|
|
|
|
str = str + i;
str += i;
str = str.concat(i);
str = [str, i].join('');
// Booleans
x = true; // true
y = !x; // false
z = 1 < 0; // false
// Comparisons
if (x) {
// success
} else {
// failure
}
result = (x) ? success : failure; // results in success
var x = 1, // number
y = '1'; // string
x == y; // true
x === y; // false
x !== y; // true
|
|
|
|
|
|
|
|
|
JavaScript | jQuery |
Math.random | jQuery.trim |
isNaN | hide |
parseInt | show |
toFixed | toggle |
indexOf | is |
substring | click |
|
|
|
|
Immediately Invoked Function Expression
|
|
|
|
|
|
Document Object Model
|
|
Chaining
|
|
|
|
|
|
jQuery('#someID').css('color', 'red');
jQuery('#someID').css('backgroundColor', 'white');
jQuery('#someID').css('border', '1px solid blue');
jQuery('#someID').addClass('chicagoCubs');
![]() |
![]() |
![]() |
|
![]() |
|
![]() |
![]() |
![]() |
|
|
|
|
performance.now
var start = performance.now(),
end, elapsed;
/* my code
* runs in
* this block */
end = performance.now();
elapsed = end - start;
console.log('MS elapsed: ' + elapsed);
![]() |
![]() |
![]() |
|
|
if (jQuery('#someID')) {...} // will always be true
if (jQuery('#someID').length) {...} // will only be true if at least one is found
jQuery.fn.exists = function() {
return this.length > 0;
}
if (jQuery('#someID').exists()) {...} // easier to understand what you are doing
// using attr function
var theID = jQuery('p').attr('id');
// converting jQuery object to JS with get
var theID = jQuery('p').get(0).id;
jQuery('button').click(function() {
// common way in jQuery
var theID = jQuery(this).attr('id');
});
jQuery('button').click(function() {
// better way using vanilla JS
var theID = this.id;
});
for (var i = 0; i < els.length; i++) {
// do something with each item of els
}
for (var i = 0, iLen = els.length; i < iLen; i++) {
// do something with each item of els
}
for (var i = els.length; i--;) {
// do something with each item of els
}
var arr = new Array();
var arr = [];
var max = Math.max.apply(Math, arr); // max has max value from arr
var min = Math.min.apply(Math, arr); // min has min value from arr
arr.length = 0; // empty array
arr.length = 4; // truncate array to 4 values
delete arr[3]; // item 4 in arr will now be undefined
arr.splice(3, 1); // item 4 is now completely gone from arr
var arr2 = arr.map(function(val) {
return val * val;
});
// arr2 now holds all the original values of arr, but squared
jQuery('button').click(function() {
alert('Button was clicked!');
});
jQuery('body').on('click', 'button', function() {
alert('Button was clicked!');
});
jQuery('button').on('click', function() {
alert('Button was clicked!');
});
jQuery('div').find('p').css('backgroundColor', 'red');
jQuery('div').find('span').css('backgroundColor', 'blue');
jQuery('div').find('p').css('backgroundColor', 'red')
.end().find('span').css('backgroundColor', 'blue');
JavaScript
typeof ''; // string
typeof 0; // number
typeof null; // object
typeof new Date(); // object
typeof []; // object
typeof {}; // object
jQuery
jQuery.type(''); // string
jQuery.type(0); // number
jQuery.type(null); // null
jQuery.type(new Date()); // date
jQuery.type([]); // array
jQuery.type({}); // object
Before Plugins | After Plugins
Questions? Comments?