Show Menu
Cheatography

MCAD Lecture 1 - Javascript 101 Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

What is Javasc­ript?:

A scripting language
Used to implement logic into web and mobile apps
Syntax similar to C++

Data Types:

- Boolean
- Null
- Undefined
- Number
- String
- Symbol
Use the ‘Var’ keyword to declare

 var x = 10; 


Data type depends on what value is assigned to it
Use
 typeof­(va­riable name); 
to output the data type

Variables Declar­ation

 var x; 
- undefined
 var x = 10; 
- number
 x = "­Hello World"; 
- string

Text Output

 consol­e.l­og(­“Hello World”);
- outputs text to the console
 consol­e.l­og(­“Hello World” + someVa­riable + “and Goodbye”);
- can concen­trate output with the ‘+’ operator
 

If Statements

if (x < 10)

{

 
consol­e.l­og(­“Less than 10”);

}

else

{

 
consol­e.l­og(‘Not less than 10”);

}

Switch Statements

switch(x)

{

 
case 4:

 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ 
consol­e.l­og(­“He­llo”);

 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ 
break;

 
case 5:

 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ 
consol­e.l­og(­“Go­odb­ye”);

 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ 
break;

 
case 6:

 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ 
consol­e.l­og(­som­eVa­ria­ble);

 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ 
break;

 
default:

 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ 
consol­e.l­og(“No Instru­cti­on”);

}

For Loop

for (i = 0; i < 10; i++)

{

 
consol­e.l­og(­“Number “ + i);

}

While Loop

while (i < 10)

{

 
consol­e.l­og(­“Number “ + i );

 
i++;

}
 

Do-While Loop

do

{

 
consol­e.l­og(­“Number “ + i );

 
i++;

}

while (i < 10);

Strings

var myString = “Hello World”;


consol­e.l­og(­myS­tri­ng.l­en­gth); -
‘length’ operator gets length of string

Functions

function functi­onName (argum­ents);

function Multiplier (v1, v2)

{

 
consol­e.o­utput (v1 * v2);

}


No return type

Arrays

Javascript arrays can contain a mix of different data types
Can access and assign values to elements that in the array yet

var oscill­ators [“sine”, “saw’, “square”];