Javascript Examples

Strings
Sample:
"I" + "Like" + "Baseball"
When I add strings in console it will appear as "ILikeBaseball" combined into 1 string.
.split
Sample:
"baseball".split ("ase")
When I use .split in the console it will remove "ase" from the string and break original string into 2 new seperate strings "b" and "ball".

Sample:
"baseball".split ()
When I use .split in the console it will it will remove nothing and leave the single string intact.
.length
Sample:
"baseball".length
When I add .length in console it will return "8" as .length counts the number of characters in a string.
.slice
Sample:
"baseball".slice (0,2)
When I add .slice in console it return "ba" as .slice returns characters of a string at that character space.
.pop
Sample:
["baseball", "homerun", "hit"].pop ()
When I add .pop to an array in console it will return "hit" as it is the last item in the array.
.shift
Sample:
["single", "double", "triple"].shift ()
When I add .shift in console it return "single" as it is the first time in the array.
.reverse
Sample:
teamstats.reverse ()
When I add teamstats.reverse () in the console it will return "50, 25, 15" as it is the reverse of strings in the array assigned to variable teamstats.
Sample Array
Sample:
["Red, Sox"].join (" ")
When I add "["Red", "Sox"].join(" ")" in the console it will combine to "Red Sox" as it combines the two strings inside the array into one string using " " to add a space between the two.
Assigning Numeric Value For Variable
Sample:
var x = 3
When I add var x = 3 in .js file and the run "x < 4" in console it will return "true" as the value of x has been previously set to 3. If I run "x > 4" it will return "false".
I can manipulate the new value of x in other algebraic equations as well. Running "3 + x" will return 6. "x + x" will return 6.
Remainder Operator
Sample:
25 % x
When I add 25 % 4 in the console it will return 1 as "%" gives the remainder once the second number is divided from the original number. Thus 25 divded by x (3) is 24 leaving a remainder of 1. If "24 % x" was entered into the console it would return "0".
True And False
Sample:
ortiz.triples == napoli.triples
When I add "ortiz.triples == napoli.triples" into the console it will return true as the value of napoli.triples and oritz.triples is 0. If I enter "napoli.triples == pedroia.doubles" into the console it will return false.
Using "If"
Sample:
if (pedroia.doubles > 10) {console.log ('Laser Show')} else {console.log ('Swing The Bat')}
When I add above if/else statement into console it will return "Laser Show" as the value of pedroia.doubles is 11..
Using "And"
Sample:
napoli.battingaverage > 0.1 && napoli.battingaverage < 0.2
The symbol for or is "&&". When I add the above satement into the console it will return false as the value of napoli.battingaverage is .290 and .290 is greater than 0.1 but NOT less than 0.2.
Using "Or"
Sample:
ortiz.battingaverage < 0.4 || ortiz.battingaverage > 0.19
The symbol for or is "||". When I add "x < 4 || x > 7" into the console it will return true as the value of ortiz.battingaverage is 0.241 and 0.241 is less than 0.4 OR greater than 0.19.
Calling For Info
Sample: ortiz.homeruns
Entering "ortiz.homeruns" will return "6" as he has hit 6 homeruns and this is attached in his variable array. Another example would be entering "pedroia.battingaverage" in the console which would return ".275" as this is the value attached in his variable array.
Interesting Data Pulls
Pull interesting info : (uehara.era + buckholz.era + doubront.era +lackey.era + lester.era + miller.era + peavy.era) / 7 ===== Pitching Average ERA