SlideShare a Scribd company logo
2
Most read
3
Most read
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!	
  
    function test() {
        if(true) {
            var x = 5;
        }
        document.write(x);
    }
•  This	
  works!	
  X	
  is	
  defined	
  in	
  the	
  scope	
  of	
  the	
  
   func1on	
  (or	
  globally)	
  
Hois1ng	
  
•  Variables	
  are	
  moved	
  on	
  top	
  of	
  the	
  func1on!	
  
function test() {
     var x;
     if(true) {
           x = 5;
     }
     document.write(x);
}
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); // undefined
    if(true) {
        x = 5;
    }
}
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;	
  
Objects	
  
var Sales = "Toyota";

function CarTypes(name) {
  if (name == "Honda")
     return name;
  else
     return "Sorry, we don't sell " + name + ".";
}

var car = { myCar: "Saturn", getCar: CarTypes("Honda"), special: Sales };

document.write(car.myCar);   // Saturn
document.write(car.getCar); // Honda
document.write(car.special); // Toyota
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 3
alert(theAdd(1,2));        // also produces 3

More Related Content

What's hot (20)

PPTX
Javascript
Sun Technlogies
 
PDF
jQuery for beginners
Arulmurugan Rajaraman
 
PDF
Lets make a better react form
Yao Nien Chung
 
PPT
JavaScript Basics
Mats Bryntse
 
PPT
Javascript
mussawir20
 
PDF
Basics of JavaScript
Bala Narayanan
 
PPT
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
PPSX
Data Types & Variables in JAVA
Ankita Totala
 
PPTX
Javascript functions
Alaref Abushaala
 
PDF
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
PPT
JavaScript - An Introduction
Manvendra Singh
 
PPTX
Introduction to JavaScript Basics.
Hassan Ahmed Baig - Web Developer
 
PDF
javascript objects
Vijay Kalyan
 
PDF
JavaScript Programming
Sehwan Noh
 
PDF
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
PPT
JavaScript Control Statements II
Reem Alattas
 
PPTX
Javascript operators
Mohit Rana
 
PDF
Java IO
UTSAB NEUPANE
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
PPT
Introduction to Javascript
Amit Tyagi
 
Javascript
Sun Technlogies
 
jQuery for beginners
Arulmurugan Rajaraman
 
Lets make a better react form
Yao Nien Chung
 
JavaScript Basics
Mats Bryntse
 
Javascript
mussawir20
 
Basics of JavaScript
Bala Narayanan
 
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
Data Types & Variables in JAVA
Ankita Totala
 
Javascript functions
Alaref Abushaala
 
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
JavaScript - An Introduction
Manvendra Singh
 
Introduction to JavaScript Basics.
Hassan Ahmed Baig - Web Developer
 
javascript objects
Vijay Kalyan
 
JavaScript Programming
Sehwan Noh
 
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
JavaScript Control Statements II
Reem Alattas
 
Javascript operators
Mohit Rana
 
Java IO
UTSAB NEUPANE
 
Exception Handling in JAVA
SURIT DATTA
 
Introduction to Javascript
Amit Tyagi
 

Viewers also liked (7)

PDF
Quick Intro to Java Collections
Jussi Pohjolainen
 
PDF
00 introduction-mobile-programming-course.ppt
Jussi Pohjolainen
 
PPT
Il&fs investsmart insurance brokers ltd jamshedpur
Anirban Chakraborty
 
PDF
Extensible Stylesheet Language
Jussi Pohjolainen
 
PDF
Android SDK: How to Install
Jussi Pohjolainen
 
PDF
Android Http Connection and SAX Parsing
Jussi Pohjolainen
 
PDF
Moved to Speakerdeck
Jussi Pohjolainen
 
Quick Intro to Java Collections
Jussi Pohjolainen
 
00 introduction-mobile-programming-course.ppt
Jussi Pohjolainen
 
Il&fs investsmart insurance brokers ltd jamshedpur
Anirban Chakraborty
 
Extensible Stylesheet Language
Jussi Pohjolainen
 
Android SDK: How to Install
Jussi Pohjolainen
 
Android Http Connection and SAX Parsing
Jussi Pohjolainen
 
Moved to Speakerdeck
Jussi Pohjolainen
 
Ad

Similar to JavaScript: Variables and Functions (20)

PPTX
Object oriented java script
vivek p s
 
PDF
Java scriptconfusingbits
ColdFusionConference
 
PDF
Java scriptconfusingbits
ColdFusionConference
 
PDF
Web 4 | Core JavaScript
Mohammad Imam Hossain
 
PDF
JS OO and Closures
Jussi Pohjolainen
 
PPTX
LinkedIn TBC JavaScript 100: Intro
Adam Crabtree
 
PPT
eXo SEA - JavaScript Introduction Training
Hoat Le
 
PDF
Fii Practic Frontend - BeeNear - laborator3
BeeNear
 
PPT
JavaScript - Programming Languages course
yoavrubin
 
PPT
Basic Javascript
Bunlong Van
 
PDF
1669958779195.pdf
venud11
 
PPTX
Javascript analysis
Uchitha Bandara
 
PPTX
Awesomeness of JavaScript…almost
Quinton Sheppard
 
PPTX
Wt unit 5
team11vgnt
 
PDF
JavaScript
Ivano Malavolta
 
KEY
The JavaScript Programming Primer
Mike Wilcox
 
PDF
Important JavaScript Concepts Every Developer Must Know
yashikanigam1
 
PDF
JavaScript Getting Started
Hazem Hagrass
 
PPTX
Lecture 4- Javascript Function presentation
GomathiUdai
 
PPTX
oojs
Imran shaikh
 
Object oriented java script
vivek p s
 
Java scriptconfusingbits
ColdFusionConference
 
Java scriptconfusingbits
ColdFusionConference
 
Web 4 | Core JavaScript
Mohammad Imam Hossain
 
JS OO and Closures
Jussi Pohjolainen
 
LinkedIn TBC JavaScript 100: Intro
Adam Crabtree
 
eXo SEA - JavaScript Introduction Training
Hoat Le
 
Fii Practic Frontend - BeeNear - laborator3
BeeNear
 
JavaScript - Programming Languages course
yoavrubin
 
Basic Javascript
Bunlong Van
 
1669958779195.pdf
venud11
 
Javascript analysis
Uchitha Bandara
 
Awesomeness of JavaScript…almost
Quinton Sheppard
 
Wt unit 5
team11vgnt
 
JavaScript
Ivano Malavolta
 
The JavaScript Programming Primer
Mike Wilcox
 
Important JavaScript Concepts Every Developer Must Know
yashikanigam1
 
JavaScript Getting Started
Hazem Hagrass
 
Lecture 4- Javascript Function presentation
GomathiUdai
 
Ad

More from Jussi Pohjolainen (20)

PDF
Java Web Services
Jussi Pohjolainen
 
PDF
Box2D and libGDX
Jussi Pohjolainen
 
PDF
libGDX: Screens, Fonts and Preferences
Jussi Pohjolainen
 
PDF
libGDX: Tiled Maps
Jussi Pohjolainen
 
PDF
libGDX: User Input and Frame by Frame Animation
Jussi Pohjolainen
 
PDF
Intro to Building Android Games using libGDX
Jussi Pohjolainen
 
PDF
Advanced JavaScript Development
Jussi Pohjolainen
 
PDF
Introduction to JavaScript
Jussi Pohjolainen
 
PDF
Introduction to AngularJS
Jussi Pohjolainen
 
PDF
libGDX: Scene2D
Jussi Pohjolainen
 
PDF
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
PDF
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
PDF
libGDX: User Input
Jussi Pohjolainen
 
PDF
Implementing a Simple Game using libGDX
Jussi Pohjolainen
 
PDF
Building Android games using LibGDX
Jussi Pohjolainen
 
PDF
Android Threading
Jussi Pohjolainen
 
PDF
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Jussi Pohjolainen
 
PDF
Creating Games for Asha - platform
Jussi Pohjolainen
 
PDF
Intro to Asha UI
Jussi Pohjolainen
 
PDF
Intro to Java ME and Asha Platform
Jussi Pohjolainen
 
Java Web Services
Jussi Pohjolainen
 
Box2D and libGDX
Jussi Pohjolainen
 
libGDX: Screens, Fonts and Preferences
Jussi Pohjolainen
 
libGDX: Tiled Maps
Jussi Pohjolainen
 
libGDX: User Input and Frame by Frame Animation
Jussi Pohjolainen
 
Intro to Building Android Games using libGDX
Jussi Pohjolainen
 
Advanced JavaScript Development
Jussi Pohjolainen
 
Introduction to JavaScript
Jussi Pohjolainen
 
Introduction to AngularJS
Jussi Pohjolainen
 
libGDX: Scene2D
Jussi Pohjolainen
 
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
libGDX: User Input
Jussi Pohjolainen
 
Implementing a Simple Game using libGDX
Jussi Pohjolainen
 
Building Android games using LibGDX
Jussi Pohjolainen
 
Android Threading
Jussi Pohjolainen
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Jussi Pohjolainen
 
Creating Games for Asha - platform
Jussi Pohjolainen
 
Intro to Asha UI
Jussi Pohjolainen
 
Intro to Java ME and Asha Platform
Jussi Pohjolainen
 

Recently uploaded (20)

PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 

JavaScript: Variables and Functions

  • 1. JS  Variables  and  Func1ons   Jussi  Pohjolainen   Tampere  University  of  Applied  Sciences  
  • 2. Datatypes   •  Numbers   •  Boolean   •  String   •  Null,  undefined   •  Object   •  Func1on  
  • 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. Hois1ng   •  Variables  are  moved  on  top  of  the  func1on!   function test() { var x; if(true) { x = 5; } document.write(x); }
  • 5. Hois1ng   function test() { document.write(x); // Prints undefined! if(true) { var x = 5; } }
  • 6. Hois1ng   function test() { var x; document.write(x); // Prints undefined! if(true) { x = 5; } }
  • 7. So  what  happens  here?   var x = 10; function test() { document.write(x); if(true) { var x = 5; } }
  • 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. 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. Objects   var Sales = "Toyota"; function CarTypes(name) { if (name == "Honda") return name; else return "Sorry, we don't sell " + name + "."; } var car = { myCar: "Saturn", getCar: CarTypes("Honda"), special: Sales }; document.write(car.myCar); // Saturn document.write(car.getCar); // Honda document.write(car.special); // Toyota
  • 11. Basic  Func1on   function add(a, b) { return a+b; } alert(add(1,2));
  • 12. Func1on  as  Variable   var add = function(a, b) { return a+b; } alert(add(1,2));
  • 13. Func1on  as  Variable   var add=function theAdd(a, b) { return a+b; } alert(add(1,2)); // produces 3 alert(theAdd(1,2)); // also produces 3