What will we cover? (content)
JavaScript Fundamentals
var oneDozen = 12;var myName = "Scott"var theSkyIsBlue = truevar macBook = {
model: "2014 Air",
price: 1400
}
nullundefined;var one = 1;varThe "var" keyword indicates the definition of a local variable
var one"var" is followed by the name of the variable
var one = 1;The name is followed by the assignment operator and an expression or value
var t3l_lo$ = "valid";var 2invalid = "invalid";var camelCase = "camelCase";var var = "invalid";var CaSeSeNsItIvE = "valid";var one = 1;
var two = one + one;var two = 2
var one = 1;
var two = one + one;
one = one + two;
var one = 3
function bakeCake(flavor){
return "This is a " + flavor + "cake!";
}bakeCake("Chocolate");bakeCake("Vanilla");alert("My Name Is Scott");This is a JavaScript Statement that invokes the alert function which accepts a string value argument
// function declaration
function alertTwoTimes(message){
alert("First Message: " + message);
alert("Second Message: " + message);
}
alertTwoTimes("Hello"); //invoking the functionif(something_is_true){
// do_something;
} else {
// nothing is true
// run this code
}function whichBrowser(browser){
if(browser === "Google Chrome"){
alert("This is Google Chrome!");
} else {
alert("Download Chrome!");
}
}
whichBrowser("Google Chrome");function returnValue(valueToReturn){
return valueToReturn;
}
returnValue("JavaScript Rocks");function exampleReturn(expression){
if(expression){
return "The expression is true";
} else {
return "The expression is false";
}
console.log(5);
}
exampleReturn(11<10);if(7 < 10){
console.log("7 is less than 10");
} else {
console.log("7 is greater than 10");
}function isBakersDozen(guess){
if(guess === 13){
return "You are Correct";
} else if(guess === 12){
return "Close! But wrong Dozen";
} else {
return "Try Again";
}
}true && true // returns truefalse || true // returns true!false || !true // returns truevar oneDozen = 12;
var twoDozens = 2 * oneDozen;Common Arithmetic Operators in JavaScript
function add(num1, num2){
return num1+num2;
}
add(5,6);function modulo(num1,num2){
return num1%num2;
}
modulo(11,5);var one = 1;one = one + 1;one++;one--;
one+=5;
one -=5;
one *= 2;
one /= 2;
one %= 3;var jump = "Jump";
var start = 'Start';var currentClass = jump + start;function whatCharacter(stringToCheck,indexInString){
return stringToCheck[char];
}
whatCharacter("HelloWorld",5);
var helloWorld = "Hello World";
helloWorld.length;helloWorld.charAt(4);"hello world".length;.length property: returns the length of the string starting from the count of 1
"hello world".toUpperCase();.toUpperCase method: returns the calling string value converted to upper case characters
.toString method: converts a value to a string value
var two = 22;
two.toString();Strings look like one cohesive unit but they are actually made up of a series of characters
Zero-Based Index (the count starts from 0 instead of 1)
Index positions start at 0 and then move up
var hello = "Hello";"Hello Fullstack".charAt(8);"Hello Fullstack"[4];charAt method: returns a character at a specified (the arguments) index value
Bracket Notation: returns the character at the specified index value.
"Hello Fullstack"[2+2];"hello world".slice(2,5);.slice method: returns a string based on the start and end point specified by the first and second argument
.indexOf method: the arg is the substring or character to search for in the string. The return value is the index location of character or string
"Hello World".indexOf("World");"Hello\t\tWorld";"Hello\n\nWorld";"HelloWorld" === "HelloWorld";Tab Spacing
New Line
Comparison
doSomething();
doSomething();
doSomething();
doSomething();
doSomething();
doSomething();
doSomething();
doSomething();
var count = 8;
for(var i = 0; i < count; i++){
doSomething();
}var count = 10;
while(count > 0){
count--;
doSomething();
}console.log("Repeat this #" + number + " 4 times");Loops allow code to be executed repeatedly.
Define a Variable
var number = 1;Create a counter to increase on every repetition of the loop
console.log("Repeat this #" + number + " 4 times");
number++;Create an expression that evaluates to true or false
number <= 4;var number = 1;
while(number <= 4){
console.log("Repeat this #" + number + " four times");
number++;
}for(var i = 0; i < 10; i++){
console.log(i);
}
1.
2.
3.
4.
function upToFive(){
for(var i = 1; i<5; i++){
console.log(5);
}
}
upToFive();