Javascript runs…
on the browser rather than the server. Whereas before in order to perform action, data and operations were being sent to the server.
Javascript is an _____ programming language
interpreted
Javascript Alerts
alert(“Text to Display Alert”);
JS Data Types
Different Types
String
Boolean
Number
typeof()
Output of function
“number”
“boolean”
“string”
JS Variables
prompt(“Dialogue to display for prompt”);
To declare a variable use, var.
var, variables can later be changed to a different value
Example:
var myName = “Anand”;
Naming and Naming Conventions for JS Variables
Variable names cannot start with a number
Variable can only contain numbers, letters, $, and/or _
Variables follow a camel-case format.
First word contains lowercase, then subsequent words contain capital letters for their names
String Concatenation
Can combine strings using +
Example:
“Hello” + “World” -> “HelloWorld”
String Lengths and Retrieving the Number of Characters
To check the number of characters in a string
stringVariable.length
Slicing and Extracting Parts of a String
Slice Function stringslice(x, y); Slices the string from x to y. Including x, up to but not including y Example: “Anand”.slice(0,3); “Ana”
Changing Casing in Text
toUpperCase() Example: “word”.toUpperCase() “WORD” toLowerCase()
Basic Arithmetic and the Modulo Operator in Javascript
Modulo % Gives the remainder of a division Example: 9 % 6 = 3
Increment and Decrement Expressions
x++
x = x + 1;
x- -
x = x - 1;
Creating and Calling Functions
Declaring functions:
function myFunction() { <code> }
Calling a function
myFunction();</code>Function Parameters and Arguments
Example:
function getMilk(money) { … }Function Outputs and Return Values
Include a return statement in the function to make it a function that has an output
Control Statements: If-Else Conditionals and Logic
if ( condition ) { < code to run, if condition is true > }
else { < code to run, if condition is false> }
Comparators and Equality
=== Is equal to Checks both for the value and the data types !== Is not equal to >, = == Only checks if the values equate to each other. Doesn’t take into account the data types So, 1 == “1”, will be equal to true
Combining Comparators
&& AND || OR ! NOT
Javascript Arrays
Creating an array Example: Empty Array var names = []; Prefilling an array with elements var names = [“Bob”, “John”, “Jennifer”]; Retrieving an element from an array Example: names[1]; // “John” Array elements always start at 0 Retrieving the number of elements in an array Array.length // Return the number of elements Checking if an array contains a specific element Array.includes( ); Returns a boolean
Adding Elements and Intermediate Array Techniques
Adding an element at the end of an array
Array.push( );
Remove and retrieve the last element of an array
Array.pop(); // Returns the last element of the array
While Loops
while ( ) { // Code to run }
For Loops
for(startingCondition ; conditionToRunLoopIfTrue; conditionModifier) { // Code to run if condition is true}
Example:
for(i = 0; i < 100; i++) { // Run code}