On Github KansasCityWomeninTechnology / JSProgrammingFundamentals
Slides available at: http://bit.ly/CNCJSPF
Placeholder to store values
<div id="name"></div>
var firstName = "Jane"; document.getElementById("name").innerHTML = firstName;
<div id="name">Jane</div>We use camelCase for variable and function naming. This is where the first word starts with a lower case letter and each subsequent word starts with a capital letter.
var globalVar = "I am globally scoped"; function scoping () { var localVar = "I am locally scoped to the function"; console.log(localVar); // "I am locally scoped to the function" console.log(globalVar); // "I am globally scoped" } console.log(localVar); // undefined console.log(globalVar); // "I am globally scoped"
function cocktailIngredients () { var firstIngredient = "Plantation Dark Rum"; var secondIngredient = "Smoked Pineapple"; var thirdIngredient = "Cinnamon Bark Syrup"; var fourthIngredient = "Lime"; var recipe; recipe = firstIngredient + ", " + secondIngredient + ", " + thirdIngredient + ", " + fourthIngredient; return recipe; } cocktailIngredients();
function cocktailIngredients (firstIngredient, secondIngredient, thirdIngredient, fourthIngredient) { var recipe; recipe = firstIngredient + ", " + secondIngredient + ", " + thirdIngredient + ", " + fourthIngredient; return recipe; } cocktailIngredients("Plantation Dark Rum", "Smoked Pineapple", "Cinnamon Bark Syrup", "Lime");
Don't Repeat Yourself - DRY Programming
Reduce Complexity & Repetition
var newElement = document.createElement('div'); newElement.className = 'navbar'; var anotherElement = document.createElement('ul'); anotherElement.className = 'menu'; var moreElements = document.createElement('li'); moreElements.className = 'menu-nav-item'; var fourthElement = document.createElement('li'); fourthElement.className = 'menu-nav-item';
function createElement(tagName, className) { var element = document.createElement(tagName); element.className = className; return element; } var newElement = createElement('div','navbar'); var anotherElement = createElement('ul','menu'); var moreElements = createElement('li','menu-nav-item');
// This is a single line comment /* This is a multi-line comment I can keep writing comments on more lines until I tell it that I'm done */
// TODO: is a typical comment to remind yourself to come back & do something
//TODO: Add examples to comment section
Add to code for debugging help
var shoeBrand = "Jimmy Choo"; console.log(shoeBrand); // Jimmy Choo var addOne = function (x) { return x + 1 } console.log(addOne) // function (x) { return x + 1 } var name = "Jane"; var cocktail = "Old Fashioned"; console.log(name); // Jane console.log(name , " wants a(n) ", cocktail, ".") // Jane wants a(n) Old Fashioned.
A number wrapped in quotes is a string!
"45" is not the same as 45
var age = 28 + 10; // 38Subtraction -
var ageITellPeople = 38 – 9; // 29Multiplication *
var myDogsAge = 6 * 7; // 42Division /
var ozWineBottleSplitFourWays = 25.4 / 4; // 6.35
var cookiesLeftAfterThreeKidsSplitEqually = 25 % 3; // 1
(As if this is a true scenario)
Increment ++var tenPlusOne = 10++; // 11Decrement --
var tenMinusOne = 10--; // 9
var twentyOne = 10 + 11; // 21Adding strings and numbers gives you a string
var textAndNumberString = ‘happy ’ + 21 + ‘ birthday’; // happy 21 birthdayGotcha!
var gotcha = '10' + 24 // 1024
Adding strings is called concatenation!
var olive = "Spanish";== Loose Equals/Double Equals
42 == "42" //true=== Strict Equal/Triple Equal
var meaningOfLife = 42; meaningOfLife === "42" //false meaningOfLife === 42 //true
var age = 62; var drink = (age > 21) ? "Wine" : "Grape Juice"; //WineCompare object types
Sometimes you want to run some code only when a condition is true or false
var usersName = ‘Heidi’; if (usersName.length > 0) { submitDrinkOrder(); } else { alert(‘Please enter your name before submitting your drink order.’); }
Else is optional!
It is possible to use an If statement by itself!
var flower = "Sweet Pea" if (flower === "Sweet Pea") { addToBouquet(); }
for (var i = 0; i < 5; i++) { notifyDrinkIsReady(i); }i is a common reference for incrementer. watch out for infinite loops!
var myArray = new Array();
var anotherArray = [];Zero-based index
var myArray = ["first","second","third"] var firstArrayItem = myArray[0] // first var secondArrayItem = myArray[1] // second var last = myArray[myArray.length – 1]; // third
var cocktails = [‘Code on the Beach’, ‘jQuery Sour’]; // Loop for (var i = 0; i < cocktails.length; i++) { console.log(cocktails[i]); } // Add item cocktails.push('Tom Collins'); // [‘Code on the Beach’, ‘jQuery Sour’, 'Tom Collins'] // Remove cocktails.splice(2,1); //[‘Code on the Beach’, ‘jQuery Sour’]
var myObject = new Object();
var anotherObject = {};
var myObject = { "keyWrappedInQuotes": *, "valueCanBeAString": "string", "valueCanBeANumber": 3, "valueCanBeABoolean": true, "valueCanBeAnArray": ["Red","White","Sangria"], "valueCanBeAnObject": { "keyInsideChildObject": "is this inception?" } "valueCanBeAFunction": function() { shutTheFrontDoor(); } }
Using object data
myObject.valueCanBeAnArray // ["Red","White","Sangria"]
Different than jQuery
document.getElementById('cocktail'); document.getElementsByClassName('red');different browsers support different DOM Selectors. This is getting better with today’s modern browsers, but if you have to support older browser versions, like IE9, libraries like jQuery will be your BFF.
Google it!
#LadyDevs
#KCWiT
#CodingAndCocktailsKC