What will we cover?
JavaScript Fundamentals
var numUSStates = 50;
var futureYear = 2029;
var cityName = "New York";varThe "var" keyword indicates we are declaring a local variable.
var oneDozen = 12;var myName = "Scott"var theSkyIsBlue = truevar macBook = {
model: "2014 Air",
price: 1400
}
nullundefined;var numCats = 2;
var numDogs = 1;
var totalPets = numCats + numDogs;
// what will the value of totalPets be?Adding Numbers
Adding Strings
var first_name = "Grace";
var second_name = "Hopper";
var full_name = first_name + second_name;
// What's the value of full_name?
var numCats = 2;
// Three ways to adding one to numCats
numCats++;
numCats += 1;
numCats = numCats + 1;
Three ways to add one
var numCats = 2;
numCats += 4;
// How many cats do we have now?
Adding more than 1
var name = "John";
/* Appending a string to the end of
another string
*/
name += " Doe";
// Name is now "John Doe"Appending Strings
++ does not work for Strings
function bakeCake(flavor, numServings){
//sample instructions for baking cake
makeBatter(numServings);
addFlavoring(flavor);
return createCake();
}bakeCake("Chocolate", 5);bakeCake("Vanilla", 18);alert("My Name Is Scott");This is a JavaScript Statement that invokes the alert function and accepts a string value argument
function doYouLikeJavaScript(){
return "You Bet I Do";
}
function whatIsYourName(theName){
return "Scott";
alert(theName);
}
var name = whatIsYourName("Jim");
alert(name);var someValue = true;
if(someValue){
console.log(true);
} else {
console.log(false);
}if(7 < 10){
console.log("7 is less than 10");
} else {
console.log("7 is greater than 10");
}Falsey: Something which evaluates to FALSE.
Falsey Values
Everything else is Truthy
function truthyOrfalsey(val){
if(val){
return "The value is TRUTHY";
} else {
return "The value is FALSEY"
}
}function isBakersDozen(guess){
if(guess === 13){
return "You are Correct";
} else if(guess === 12){
return "Close! But wrong Dozen";
} else {
return "Try Again";
}
}var skyColor = 'gray';
var isThundering = false;
if (skyColor === "gray" && isThundering === true) {
console.log("It's Raining!");
}
var oneDozen = 12;
var twoDozens = 2 * oneDozen;Common Arithmetic Operators in JavaScript
function add(num1, num2){
return num1+num2;
}
add(5,6);var jS = "JS";
var jump = ' Jump';
var start = 'Start';var currentClass = jS + jump + start;"JS JumpStart"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 increment by 1
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".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
"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("W");