Internet of Things
TinkerforgeCode in ActionAbout Me
  • Course Outline
  • 1 - Getting Started
    • Overview
    • Connect to the LED
    • Getting Started
      • Glitch
      • The Application Template
    • Concepts in Programming
      • What is Programming?
      • Variables
      • Functions and Commands
      • Control Structures
      • Loops
      • Objects and Libraries
    • Programming Simple Web Apps
    • Exercises
      • 1.1 Buttons and Inputs
  • 2 - Internet of Things
    • Overview
    • IoT in our Apps
      • Getting Started
        • Hardware Kit
        • Brick Viewer and Daemon
      • Connect to the Devices
        • The Tinkerforge Device Manager
      • Program the Devices
        • RGB LED
        • RGB LED Button
        • OLED Display
        • Sensors
          • Humidity Sensor
          • Ambient Light Sensor
    • Components and Use Cases
    • Exercises
      • 2.1 Lights and Buttons
      • 2.2 Sensors
      • 2.3 Display
  • 3 - Artificial Intelligence
    • Overview
    • AI in our Apps
      • Google's Teachable Machine
      • Face Recognition
      • Training a Custom Model
    • Rules vs. Learning
    • Learning from Data
    • Use Cases
      • Computer Vision
        • Image Classification
        • Handwriting Recognition
    • Machine Learning Algorithms
      • Artificial Neural Networks
      • Decision Trees
      • Logistic Regression
    • Exercises
      • 3.1 Rules vs. Learning
      • 3.2 Fruits and Vegetables
      • 3.3 Face Recognition
      • 3.4 A Classifier for Iris
  • 4 - Cloud & APIs
    • Overview
    • APIs in our Apps
    • Cloud and APIs
      • Weather API
      • NASA Open APIs
      • EDAMAM Nutrition and Recipes API
    • Push Notifications
    • Exercises
  • 5 - App Project
    • Overview
    • Summer 2021
    • Summer 2022
  • Appendix
    • Other Devices
      • Motorized Linear Poti
      • Sound Pressure Sensor
      • NFC Reader
      • Motion Detector
    • UI Features
      • Realtime Charts
      • Countdown Timer
    • Digital Computers
      • Overview
      • The Binary System
      • Code Systems
      • Logic Gates
      • Binary Addition
      • From Analog to Digital
    • Cheat Sheets
    • Projects
      • IoT @ Pickup-Boxes
Powered by GitBook
On this page
  • Goals
  • What is a variable?
  • How do we declare a variable?
  • What can we put into a variable?
  • How do we put stuff into a variable?
  • Local and global variables

Was this helpful?

  1. 1 - Getting Started
  2. Concepts in Programming

Variables

PreviousWhat is Programming?NextFunctions and Commands

Last updated 4 years ago

Was this helpful?

Goals

  • You know what a variable is.

  • You know what we can put into a variable.

  • You can declare variables and change their values.

What is a variable?

You can think of a variable as a container, such as a box. You can put stuff into a variable, just like you can put stuff into a box. In addition, the box has a label on it, which is the name of the variable. The fact that our box has a name is very useful: at any time, we can access the content of the box by calling its name. We'll see later how we can access the content of variables that we use in our programs.

How do we declare a variable?

In line 1, the variable's name is devices. Finally, we tell our program that a statement is complete by adding a semicolon to the end of it. Great, we just declared our first variable! But ... what is a variable?

var devices;
var led;

What can we put into a variable?

The short answer is: almost anything. A variable can contain simple things like text or numbers. But it can also contain more complex things, like lists, functions or objects. We'll learn more about that later in this course.

How do we put stuff into a variable?

To answer this question, we step forward into line 22 of our program:

devices = newDevices;

The symbol = is how we assign a value to a variable. The above statement stores whatever the variable (or container) newDevices contains on the variable devices, which we declared at the beginning of our program. When you think of the two variables newDevices and devices as containers, like a bucket, the statement above effectively puts the bucket with the name newDevices into the second bucket with the name devices. Afterwards, we can still reference both buckets by their names, but we'll get the same content for both.

Local and global variables

We define the variable devices as a so-called global variable. A global variable is defined outside any function, and it is good practice to declare them at the top of your program, so we can easily find them.

The difference between a global and a local variable is their visibility. While we can access global variables from anywhere in our program, a local variable can only be used in the block where we defined it. This could be a function, for example. If you know we need a variable only within a function, it is good practice to define it locally within that function.

To stay in the container analogy, a global variable is a container that is known by everyone and everywhere. If you tell someone the container's name, everyone will know what you are talking about, and you can access the content of the container from everywhere. In contrast, a local variable is a container that is only known in a specific room. If you ask someone outside that room, she won't know what you are talking about. This also means that if you need the content of that container, you'll need to be in that room.

Read more about variables in JavaScript under the following link:

In JavaScript, we use the keyword var to tell our program that we are about to declare a new variable. The keyword var is followed by a name that we choose for our variable. The name is up to us, but we must follow .

some conventions
Storing the information you need — Variables - Learn web development | MDN
Think of variables as containers like a box (Source: https://developer.mozilla.org)
Logo