And lastly here's a great article on some of the top JavaScript resources, from Medium its includes articles, videos, and GitHub repositories.
function neatStuff () {code block};
A function is a block of code designed to perform a certain task. A JavaScript function is run when it is called (invoking the function). A JavaScript function is defined with the function keyword, followed by a name, followed by (). Function names can contain letters, numbers, and certain symbols. the parentheses may include parameter names separated by commas. The code to be executed is placed inside {}.
var thisIsAnObject = {key:value, key:value...};
Objects are containers similar to variables in JavaScript, except they can assign many values to a variable. The values are written as name:value pairs. The name:value pairs in JavaScript objects are called properties. The object properties can be accessed in two different ways. The first is using dot notation; objectName.propertyName. And the second is using bracket notation; objectName["propertyName"].
var thisIsAnArray = [item1, item2, item3...];
Arrays in JavaScript are yet another container that are similar to variables. But they can hold more than one value at a time all under a single name (like a list), always separated by commas and stored within square brackets. The values can be accessed by referring to an index number. The code above is the easiest way to create a JavaScript Array, also referred to as an array literal.
Loops are especially handy when you want to run the same code over and over, each time resulting in a different value. There are many differnt kinds of loops. There are;
for loops: which loops through a block of code a number of times.
for(statement1; statement 2; statement 3) {code block
};
for in loops: which loops through the properties of an object. for(property in object ) {code block
};
while loops: which execute a block of code, as long as a specified condition is true. while(condition) {code block
};
do/while loops: these are a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat as long as the condition is true. do {code block
}
while(condition);
Often when code is written, you want to perform different actions for different decisions. You can use conditional statements, in your code to do this. In JavaScript there are the following types of conditional statements;
if statements: these are used to specify a block of code to be executed, if a specified condition is true.
if (condition) {code block
};
else statement: this is used to specify a block of code to be executed , if the same condition is false. if (condition) {code block
} else {code block if false
};
else if: this is used to specify a new condition if the first condition is false. if (condition1){first code block if condition condition 1 is true
} else if (condition2) {second code block if condition 1 is false
} else {code to be executed if condition 1 and condition 2 is false
};
switch statements: this is a different kind of conditional statement compared to the if,else if,else statements. The switch expression is evaluated once, the value of each expression is compared with the values of each case, if there is a match the associated block of code is executed. A break keyword is used to break out of the switch block once a condition is met (these can save a lot of time because it ignores the execution of the rest of the code in the switch block), it is not necessary to break the last case in a switch block, the block ends there anyway. Also a default keyword is used to specify the code to run if there is no match. The default case does not have to be the last case in a switch block, if it's not the last case it must be followed with a break keyword. switch (expression) {
case: 1
code block
break;
case: 2
code block
break;
default:
code block
};
And thats about it! For more info on Javascript, check out the links in the sidebar and the article linked at the bottom of the page.
And lastly here's a great article on some of the top JavaScript resources, from Medium its includes articles, videos, and GitHub repositories.