Text
"JavaScript is a high level, dynamic, untyped, and interpreted programming language."
Wikipedia -
> 1 + 2;
3
//Try the following:
> 4 - 3;
> 5 * 6;
> 10 / 2;
> 9 / 2;
> 9 % 2;
> 7 + 8 * 9;
> (7 + 8) * 9;
> 0/0;
> 0/4;
> var someNumber = 42;
//For reference
> var num1 = 42; //assigns 42 to num1
> var num2 = 12; //assigns 12 to num2
> var num3 = num1 + num2; //adds num1 and num2 and assigns it to num3.
> var num4 = num3; //assigns num4 the value of num3.
> num1 = 21; //num1's new value is 21.
> num1; //This will display the value of num1 (21) to the console.
> 2345.67.toExponential();
"2.34567e+3"
> 1.3368e3.toFixed();
"1337"
toFixed() will round to the nearest whole number
> 1234.5.toFixed(2);
"1234.50"
Here's how we tell how many decimal places to use:
> 4.12345.toPrecision(4);
"4.123"
toPrecision() defines how many total digits to display of the number.
> var someNumber = 1.337134
> someNumber.toPrecision(4);
"1.337"
You can also call a method on a variable, since the variable is a placeholder for the number assigned to it.
//For reference
> 456.2356.toFixed(2); //Will display "456.24"
> 123.123.toPrecision(5); // Will display "123.12"
> 135.6236.toExponential(); //Will display "1.356236e+2"
/*The values returned by the methods are strings. I will explain strings (characters inside quotes)
in the next slide. For now, if you stored the string value returned by the methods above into
a variable, you will need to convert that value into a number.
e.g. a = "123"; //a is being assigned a string, not a number.
a = parseFloat(a); //this will convert the string to a number. Now you can call number methods on this.
*/
> "Hello world!";
> var myString = "Strings can contain characters like @, $, and %.";
> "supercalifragilisticexpialidocious".toUpperCase();
> var word = "Biggie";
> word.concat("Smalls");
We can call a method on a variable assigned to a string:
> "Tupac".concat("Shakur").toUpperCase();
We can even chain methods:
> "coding temple".charAt(5);
"g"
String methods can also take numbers as arguments for certain methods:
Computers start counting at zero, hence the reason why "g" was displayed instead of "n".
More about string methods here:
https://goo.gl/UZBEqn
//For Reference
> var ct = "coding temple"; //this will assign "coding temple" to variable ct.
> var hw = "hello world"; //this will assign "hello world" to variable hw.
> ct.toUpperCase(); //this will return "CODING TEMPLE". It will NOT change the value of ct.
> hw.concat(ct); //this will return "coding templehello world". It combines two strings together.
> hw + " " + ct; //this will return "coding temple hello world". It does the same as above, but adds a space between.
> alert("THE DOW DROPPED MORE THAN 1000 POINTS!");
> prompt("What is love?");
Try this:
> var favoriteSong = prompt("What is your favorite song?"); //I'll type "Barbie Girl"
> favoriteSong;
"Barbie Girl"
Try this:
> confirm("Annie, are you ok? Are you ok, Annie?");
//+1 for those who know the artist.
> var greeting = function() { alert('Greetings from Coding Temple!'); };
> greeting();
> var giveAlert = function(theMessage) { alert(theMessage); };
> giveAlert("YOUR COMPUTER HAS BEEN INFECTED WITH A VIRUS. DOWNLOAD SECURITY SUI...lol jk.");
> var add = function(num1, num2){ return num1 + num2; };
> add(2, 3);
> var add = function(num1, num2){ return num1 + num2; };
> add(2, 3);
//For reference
> var add = function(num1, num2){ return num1 + num2; };
> add(2, 3);
Codecademy
Coursera
(starts November 2015)
Khan Academy
JavaScript and JQuery: Interactive Front-End Web Development
Coding Temple
Intermediate JS Workshop
> var colors;
> colors = ['white', 'black', 'custom'];
> colors[0]; //this will return "white". Indexing is off by one.
> var numbers = [1,2,3,4];
> numbers.forEach(function(element){ console.log(element * element);})
//^The above will print out the square of each element.
forEach() executes a provided function once per array element.
var numbers = [1,2,3,4];
for (var i = 0; i < numbers.length; i++){ console.log(numbers[i] * numbers[i]);}
^The above accomplishes the same functionality as forEach.
for() loop executes code within square brackets until a condition is reached.