You are on page 1of 8

JavaScript

Tuesday, November 17, 2020 3:28 PM

Input -> Processing -> Output


Processor -> RAM -> HardDrive

HardDrive: Stores data permanently


• Hard drive use info from the RAM
RAM: A Temporarily Storage "When Shutting down everything get removed"
• Much more faster than hard drives
• Higher RAM higher programs you can run at the same time
• Also called Splashback
• URL used by RAM
• JavaScript and any code is run via RAM

Basics Of JS:
• Any Line of Code is Called Statement
• Each Stmt should end with a semi-colon ;
• document.write ( 'Welcome!' );
• How to add Comments?
○ Multi-Line Comments eg for Paragraphs
§ /* comments words etc */
○ Single-Line Comments eg for words in one-line
§ // …comment
○ Set Function Name to quantity
§ var quantity;
§ Function name can start with anything such as $ sign but not numbers
§ Mostly Starts with Alphabets or $ in some cases

// is a single-line comment
/* Multi-Line Comment */
Js is a Case-Sensitive
s
// is a single-line comment
/* Multi-Line Comment */
Js is a Case-Sensitive
Var quantity; // Var is a Variable Keyword, and Quantity is a Variable Name "Also Calle
• Setting Values
○ Quantity = 3; // = is an assignment operator

Types of Data
• Numbers
○ 0.75
• String
○ 'Hi, Ivy' or 'My-Name'
○ Can use Single Quite or Double ' ' or " "
• Boolean
○ True or False
○ Be Careful if written like 'True' this is STRING cuz the single-quite is used!!!

Var pi = 3.14; // First Way

Var pi;
Pi = 3.14; // Second Way Involve two-lines

Both Are Equivalent Called Arrays

Var lang = "Python 3"; // We're setting new variable called lang stores STRING

Arrays

Colors = ['Pink' , 'Yellow' , 'Green']; // Remember first element = 0, but the length tho

Colors [0]; // Used to access a specific element

Var width = 3;
Var height = 2;
Area = width * height;
// If you put any numbers between quotes won't get result since it converted to STRING
// Mathematical Operations only happens when there are Numbers Type of Data
ed Identifier"

ough is 3

G
Var width = 3;
Var height = 2;
Area = width * height;
// If you put any numbers between quotes won't get result since it converted to STRING
// Mathematical Operations only happens when there are Numbers Type of Data

Contacting Operator: Plus Sign + between String = Howdy Molly

JS Function
Var el = document.getElementByID('cost');
El.textcontent = '$' + total;

….
Colors.log("Welcome to CP 202");

What does Console.log do? It Outputs the Value

To get sum of an arrays or to calc avg:

Var marks = [9, 9, 7, 9, 5];


Var sum = marks[0] + marks[1] + marks[2] + marks[3] + marks[4];
Console.log(sum);
Console.log(marks.length);
Var avg = sum / marks.length;
Console.log (avg);

Multiplication, Subtraction & Division


13+1 * 5 // Then the order will be 5*1 then adding 13
(13+1)*5 // Addition will be done first bc of the brackets

Global Variable vs Local Variable


• GV
○ subtotal = (13*5)
• LV
○ var subtotal = (13*5)
• Var shipping = 0.5 * (13+1); // inside the bracket is done first!!

Functions:
• Makes our life easier, makes the code reusable
G
• Var shipping = 0.5 * (13+1); // inside the bracket is done first!!

Functions:
• Makes our life easier, makes the code reusable
• Let you group a series of statements together to perform a specific task
• Function should not have lot of code "Maximum of one-page screen

Declaring a Function:

function sayHello( ) {
Document.write ( 'Hello');
}

Keyword, FunctionName, { … } Block of Code

- Calling the Function


sayHello ( );

Function getArea (width, height) { // These are the type of parameter t


return width * height; // return is used to send data back to
}

Function getArea (w,h) {


Return w * h ;
}

ORDER DOES MATTER W,H when entering data to the function

The Parameters here are w,h "From Function Perspective"


When sending data to w,h function: these are called arguments "User Perspective"
the function is expected
o the line where the function were called

You might also like