Lecture 2: JavaScript Operators, Conditional Statements, Loops, Functions, Scopes
JavaScript Operators
- Binary Operators (Arithmetic)
+ - * / %- Assignment operators
= += -= *= /= %=- String operator (concatination)
+ +=- Unary operators
++ -- - ! typeof- Comparison operators
==(equal value) ===(equal value and type) != !==(different value or type) > < <= >=- Logical Operators
&& (and) || (or) ! (not)- Conditional operator (Ternary Operator) ? :
3>2 ? "true" :"false"- Bitwise operators & (AND):
x = 2 & 1; y = 3 & 1; //same as -> (10 & 01) = 00, 11 & 01 = 011 & 1 = 1, 1 & 0 = 0, 0 & 1 = 0, 0 & 0 = 0 | (OR):
x = 2 | 1; //same as -> (10 | 01) = 111 & 1 = 1, 1 & 0 = 1, 0 & 1 = 1, 0 & 0 = 0
~ (NOT):
x = ~2; // inverts the bits^ (XOR) 1 ^ 1 = 0, 0 ^ 0 = 0, 0 ^ 1 = 1, 1 ^ 0 = 1
4<<2 //shift binary version of number 4 to left by 2 bits >> Right shift:
4>>2 //shift binary version of number 4 to right by 2 bits
Conditional Statements
- if & elsevar x = Number(prompt("Enter a number")); if(!isNaN(x)) alert("You are good to go"); else alert("Hey, that's not a number");
- if, else if, else
var num = Number(prompt("Pick a number", "0")); if ( num < 10) alert (" Small ") ; else if(num < 100) alert("Medium") ; else alert("Large") ;
var firstName; if (firstName) { console.log('Your name is ' + firstName); }
- switch case
switch(prompt("What is the weather like? (enter 1 for rainy, 2 for sunny)")) { case "1": console.log("Take an umbrella with you"); break; case "2": console.log("Take a water bottle with you"); break; default: console.log("Unknown weather type!"); break; }
Loops
- Loops allow us to execute a block of statements repeatedly (till a given condition is true) - The while Loop
while(condition) { //code to be executed repeatedly ... }-The for Loop
for (initialize; condition; update) { // statements to repeat ...... }- The do while Loop
do { //code to be executed repeatedly ... }while(condition);
- Exercise Write a program which scans an input repeatedly. The program should not be repeated if the user gives a blank input. Do it using all three kinds of loops.
-Breaking out of a loop Exiting a loop even though the check condition is true
for(var i=1;i<=10;i++){ var input=prompt("Enter your name"); if(input==0) break; }- Continue a loop Continue a loop without executing remaining statements in the block.
for(var i=1;i<=10;i++){ var input=prompt('Enter "Ram" or "Rawan"'); if(input=="Ram") continue; alert("Rawan was ten headed"); }
Functions
- A block of code with or without name which can be executed when it is called (or invoked) - May or may not have a return statement. - May or may not have parameters. The scope of parameters is limited inside the functionfunction functionName([parameter1, parameter2....]) { code block....... [return();]}- Functions can be executed right when it is declared function([parameter1, parameter2....]) { code block.......})([arg1,arg2...]);
(function() { alert('foo'); })();
- anonymous functions can be assigned to variables
var VariableName = function([parameter1, parameter2....]) { code block....... [return();] };- Invoke a function
functionName([arg1,arg2....]); or VariableName([arg1,arg2....]) or invoke it with some event onclick="functionName([arg1,arg2....]);"
- Functions can be passed as arguments
var subs = function (a,b){ return(a-b); } function multi(a,b){ return(a*b); } execute(subs,10,3); execute(multi,5,9); function execute(func,a,b){ console.log(func(a,b)); }
- Recursive function
function factorial(n){ if(n==1) return 1; else return(n * factorial(n-1));}
Scopes
- Variables declared with "var" keyword inside a function and parameters are in local scope - Variables used inside a function without being declared using "var" keyword are in global scope - Only functions create new scope in javascript. Loops or other block of codes don'tvar x="global"; (function(){var x = "local";})(); console.log(x) ;
var f2 = function(){ x="I have changed the global variable"; }; f2(); console.log(x);
- Exercise Make a simple calculator that performs addition, substraction, multiplication, division. Also set a power function for "^" sign. Eg. 2^4= 2x2x2x2 = 16. Use recursive function for the power function.