JS	  Variables	  and	  Func1ons	              Jussi	  Pohjolainen	  Tampere	  University	  of	  Applied	  Sciences	  
Datatypes	  •    Numbers	  •    Boolean	  •    String	  •    Null,	  undefined	  •    Object	  •    Func1on	  
Variable	  scope	  •  Global	  and	  local	  variables	  •  JS	  does	  not	  have	  block	  statement	  scope!	      func...
Hois1ng	  •  Variables	  are	  moved	  on	  top	  of	  the	  func1on!	  function test() {     var x;     if(true) {       ...
Hois1ng	  function test() {    document.write(x); // Prints undefined!    if(true) {        var x = 5;    }}
Hois1ng	  function test() {    var x;    document.write(x); // Prints undefined!    if(true) {        x = 5;    }}
So	  what	  happens	  here?	  var x = 10;function test() {    document.write(x);    if(true) {        var x = 5;    }}
So	  what	  happens	  here?	  var x = 10;function test() {    var x; // Overrides the global one..    document.write(x); /...
Global	  Objects	  •  Global	  variables	  are	  in	  fact	  proper1es	  of	  the	     global	  object!	  •  	  In	  web	 ...
Objects	  var Sales = "Toyota";function CarTypes(name) {  if (name == "Honda")     return name;  else     return "Sorry, w...
Basic	  Func1on	  function add(a, b){  return a+b;}alert(add(1,2));
Func1on	  as	  Variable	  var add = function(a, b){  return a+b;}alert(add(1,2));
Func1on	  as	  Variable	  var add=function theAdd(a, b){  return a+b;}alert(add(1,2));           // produces 3alert(theAdd...
Upcoming SlideShare
Loading in...5
×

JavaScript: Variables and Functions

790

Published on

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
790
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
18
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

JavaScript: Variables and Functions

  1. 1. JS  Variables  and  Func1ons   Jussi  Pohjolainen  Tampere  University  of  Applied  Sciences  
  2. 2. Datatypes  •  Numbers  •  Boolean  •  String  •  Null,  undefined  •  Object  •  Func1on  
  3. 3. Variable  scope  •  Global  and  local  variables  •  JS  does  not  have  block  statement  scope!   function test() { if(true) { var x = 5; } document.write(x); }•  This  works!  X  is  defined  in  the  scope  of  the   func1on  (or  globally)  
  4. 4. Hois1ng  •  Variables  are  moved  on  top  of  the  func1on!  function test() { var x; if(true) { x = 5; } document.write(x);}
  5. 5. Hois1ng  function test() { document.write(x); // Prints undefined! if(true) { var x = 5; }}
  6. 6. Hois1ng  function test() { var x; document.write(x); // Prints undefined! if(true) { x = 5; }}
  7. 7. So  what  happens  here?  var x = 10;function test() { document.write(x); if(true) { var x = 5; }}
  8. 8. So  what  happens  here?  var x = 10;function test() { var x; // Overrides the global one.. document.write(x); // undefined if(true) { x = 5; }}
  9. 9. Global  Objects  •  Global  variables  are  in  fact  proper1es  of  the   global  object!  •   In  web  pages  the  global  object  is  window  •  So   –  var  x  =  5  ó  window.x  =  5;  
  10. 10. Objects  var Sales = "Toyota";function CarTypes(name) { if (name == "Honda") return name; else return "Sorry, we dont sell " + name + ".";}var car = { myCar: "Saturn", getCar: CarTypes("Honda"), special: Sales };document.write(car.myCar); // Saturndocument.write(car.getCar); // Hondadocument.write(car.special); // Toyota
  11. 11. Basic  Func1on  function add(a, b){ return a+b;}alert(add(1,2));
  12. 12. Func1on  as  Variable  var add = function(a, b){ return a+b;}alert(add(1,2));
  13. 13. Func1on  as  Variable  var add=function theAdd(a, b){ return a+b;}alert(add(1,2)); // produces 3alert(theAdd(1,2)); // also produces 3
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×