A Smarter Way to Learn JavaScript Flashcards
Chap 1: alert
alert ( “Thanks for your input!” );
String
Chap 2:Variables for Strings
var name = “Bobby”;
name = “Bobby”;
alert(name);
Variables are never enclosed in quotes, and text strings are always enclosed in quotes
Chapter 3:Variables for Numbers
You can also assign a number: example... 1 var originalNum = 23; 2 var numToBeAdded = 7; 3 var newNum = originalNum + numToBeAdded + 34; alert(newNum);
Chapter 4:Variable Names Legal and Illegal
Here are the rest of the rules:
- A variable name can’t contain any spaces.
- A variable name can contain only letters, numbers, dollar signs, and underscores.
- Though a variable name can’t be any of JavaScript’s keywords, it can contain keywords.
- For example, userAlert and myVar are legal.
- Capital letters are fine, but be careful. Variable names are case sensitive. A rose is not a Rose. If you assign the string “Floribundas” to the variable rose, and then ask JavaScript for the value assigned to Rose, you’ll come up empty. I teach the camelCase naming convention. Why “camelCase”? Because there is a hump or two (or three) in the middle if the name is formed by more than one word. A camelCase name begins in lower case. If there’s more than one word in the name, each subsequent word gets an initial cap, creating a hump. If you form a variable name with only one word, like response, there’s no hump. It’s a camel that’s out of food. Please adopt the
camelCase convention. It’ll make your variable names more readable, and you’ll be less likely to get variable names mixed up.
-Examples:
userResponse
userResponseTime
userResponseTimeLimitresponse
Chapter 5:Math expressions: Familiar operators
Math expression: + - * /
1 var num = 10;
2 var anotherNum = 1;
3 var popularNumber = num + anotherNum + 5;
4 alert(popularNumber);
- % is the modulus operator. It gives you the remainder when the division is executed
- Ex: var numberOne = 9 % 3; (=0)
Chapter 6: Unfamiliar operators
Unfamiliar operators: num++; <=> num = num + 1; num--; <=> num = num - 1; var n = 1; var m = n++; => m =1 ; n=2 var k = n--; => k=1 ; n=0 var l= ++n; => l=n=2 var j= --n; => j=n=0
Chapter 7: Eliminating ambiguity
In JavaScript as in algebra, the ambiguity is cleared up by precedence rules. var n = 2 * 3 + 4; =10
Chapter 8: Concatenating text strings
1 var message = "Thanks, "; 2 var userName = "Susan"; 3 var banger = "!"; 4 var customMess = message + userName + banger; 5 alert(customMess);
Chapter 9: Prompts
A prompt box asks the user for some information and provides a response field for her answer. 1 var question = "Your species?"; 2 var defaultAnswer = "human"; 3 var spec = prompt(question, defaultAnswer); 4 alert(spec);
Chapter 10: if statements
var n = prompt("What is your name?"); var m = "Bobby"; if (n === m) { alert("Exactly"); }
Chapter 11: Comparison Operators
=== , !== , >< , >= , <= is the comparison Operators
used in numbers, strings, variables, math expressions, and combinations.
Chapter 12:if…else and else if statements
In the style convention I follow, the else part has exactly the same formatting as the if part.As in the if part, any number of statements can execute within the else part.
else if is used if all tests above have failed and you want to test another condition
var n = prompt(“What is your name?”);
var m = “Bobby”;
if (n === m) { alert(“Exactly”); }
else if (n === “Bao”) { alert(“True”); }
else { alert(“No”); }
Chapter 13: Testing sets of conditions
&& (and) test for a combination of conditions in
JavaScript.
|| (or)
if (age > 65 || age < 21 && res === “U.S.”) {
Chapter 14: if statements nested
For readability, a lower level is indented 2 spaces beyond the level above it. (Nested ifs) if (x === y || a === b) && c === d) { g = h; } else { e = f; } equal if (c === d) { if (x === y) { g = h; } else if (a === b) { g = h; } else { e = f; } else { e = f; }
Chapter 15: Arrays
An Array is For readability, a lower level is indented 2 spaces beyond the level above it. var n = [1, "m", true]; var m = n{0]; (=1)
Chapter 16: Arrays Adding and removing elements
An empty Array: var n = [];
Assign value to it: n[1] = “m”; n[2] = “k”;
pop: you can remove the last element of an array
n.pop();
push: you can add value n.push(“m”, “k”);
Chapter 17: Arrays Removing, inserting, and extracting elements
var n = [“l”, “m”, “k”];
Use the Shift method to remove an element from the beginning of an array. n.shift();
To add one or more elements to the beginning of an array, use the Unshift method. n.unshift(“h”, “a”);
Use the Splice method to insert one or more elements anywhere in an array,while optionally removing one or more elements that come after it n.splice(1, 1, “a”, “b”);
=> n = [“l”, “a”, “b”];
You could make additions without removing any elements. n.splice(1, 0, “a”, “b”); => n = [“l”, “a”, “b”, “m”, “k”];
You can also remove elements without adding any. n.splice(1, 0);
Use the Slice method to copy one or more consecutive elements in any position and put them into a new array
var m = n.slice(1, 3); => m = [“m”, “k”];
Chapter 20:for loops nested
var firstNames = ["BlueRay ", "Upchuck ", "Lojack ", "Gizmo ", "Do-Rag "]; var lastNames = ["Zzz", "Burp", "Dogbone", "Droop"]; var fullNames = []; for (var i = 0; i < firstNames.length; i++) { for (var j = 0; j < lastNames.length; j++) { fullNames.push(firstNames[i] + lastNames[j]); } }
Chapter 21: Chaning Case
var n = m.toLowerCase(); or UpperCase. m can be a string, an array value (m[1]),