Loops in JavaScript are used to execute the same statement multiple times.
The for loop is most often used when dealing with collections. It is possible to not even execute if the condition evaluates to true.
for (
statement 1;
statement 2;
statement 3;
){
//Code Block
}
var x = [1, 2, 3, 4];
for(var i = 0; i < x.length; i++){
console.log(x[i]);
}
var x = [1, 2, 3, 4];
var i = 0;
for(; i < x.length; i++){
console.log(x[i]);
}
var names = ['Jon', 'Benjamin', 'Kristin'];
var html = "<ul>"
for( var i = 0; i < names.length; i++){
html = html + '<li>' + names[i] + '</li>'
}
html = html + '</ul>';
var names = [
{ firstname: 'John', lastname: 'Doe'},
{ firstname: 'Jane', lastname: 'Doe'}
];
var html = "<ul>"
for( var i = 0; i < names.length; i++){
html = html + '<li>' + names[i].firstname + ' ' + names[i].lastname + '</li>'
}
html = html + '</ul>';
var array = [1,2,3,4,5]
for(var i = array.length;i--;){
}
Clever shorthand where the 2nd statement will eval to false when i === 0.
Do and While loops are easier to read, but require more care to code properly.
May not execute depending on the condition.
var i = 0;
while(i < 25){
console.log('i is equal to ' + i);
i++
}
Will always execute at least once.
var i = 0;
do{
console.log('i is equal to ' + i);
i++
}
while (i < 25);
var condition = false;
var i = 0;
while(condition == false){
if (i > 10){
condition = true;
}
i++
}
Scope is the collection of variables, objects and functions you have the ability to access.
Local scope is a group of items that can only be accessed within the context it is declared.
function testing(){
var x = 0;
console.log(x); //logs 0
}
testing();
console.log(x); // Causes an error
Global scope items can be accessed by all JavaScript on the page.
var x = 0;
function testing(){
console.log(x); //logs 0
}
testing();
console.log(x); //logs 0;
Dates are variable types that represent a date and time inside of JavaScript.
Dates are stored as the number of milliseconds since January 1, 1970.
Example: 4/5/2017 is roughly - 1491415025702
Example : 1/1/1777 is roughly - -6090400800000
//Creates a date variable that is set to now;
var now = new Date();
// Wed Apr 05 2017 12:57:05 GMT-0500 (Central Daylight Time);
var newDate = new Date(1491415025702);
//Tue Aug 17 2010 12:09:36 GMT-0500 (Central Daylight Time)
var newDate = new Date("2010/08/17 12:09:36");
//Fri Jan 01 2010 00:00:00 GMT-0600 (Central Standard Time)
var newDate2 = new Date("1/1/2010");
condition
?
expression1
:
expression2
var isValid;
if (name.length > 0){
isValid = true;
}else{
isValid = false;
}
var isValid = (name.length > 0) ? true: false;
Simple Validation
var isValid = (name.length > 0) ? true: false;
Age Gate
var oldEnough = (age > 17) ? true: false;
var userType = (isRegistered) ? "Existing User": "New User";
You can access styles and change an element's styles inside of css.
var elm = document.getElementById('id');
elm.style.color = '#000000';
elm.style.padding = '20px';
#div{
background-color:#fff;
margin-bottom: 20px;
}
var elm = document.getElementById('div');
elm.style.backgroundColor = '#fff';
elm.style.marginBottom = '#fff';
See the Pen UA-ITR-JS-Styles by Jon (@Middaugh) on CodePen.
classList
The class list property is used to add and remove classes from HTML elements. It acts like an array, but the methods are easier to understand
classList
Methods.add('class1', 'class2', ..)
- Adds one or more classes to the class list.contains('class')
- returns true
or false
if the element contains the class.
classList
Methods Continued.item(index)
- Returns the index of the class if it is inside the element list.remove('c1', 'c2', ....)
- removes one or more classes from an element.toggle('c1')
- Adds or removes class based on if it exists or not
var elm = document.getElementById('id');
elm.classList.add('new-class'); //Adds the new class
console.log(elm.classList.contains('new-class')); //Outputs true
elm.classList.remove('new-class'); //Adds the new class
console.log(elm.classList.contains('new-class')); //Outputs false
See the Pen UA-ITR-JS-AddRemoveClasses by Jon (@Middaugh) on CodePen.
Event listeners allow us execute JavaScript when the user interacts with the document.
The safest way to execute JavaScript is to wait for the document to load, and then execute your JavaScript.
<body onload="load()">
We may not always have access to the HTML though.
if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading"){
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
This ensures your code is consistency executed wherever it is placed.