🖥️
Intro to Computer Science (ICS3U/C)
  • An Introduction to Computer Science
  • Videos & Slides
  • Unit 1: In the Beginning
    • The History of Computers
    • Binary & Logic
      • Bits and Bytes (Binary)
      • Transistors (Changing Bits)
      • Logic Gates
        • Poster
        • Logic.ly
    • The Parts of a Computer
  • Unit 2: Intro to Code
    • How Do We Code?
      • Coding Conventions (Rules)
      • Commenting Code
    • What is HTML?
      • Hello World! (in HTML)
      • HTML Slideshow
    • Hello World!
    • Input / Output
      • The Console
      • Prompt, Alert, Confirm
    • Variables & Data
      • Strings (Text)
      • Numbers (Values)
        • Converting & Rounding
        • The Math Object
          • Random Numbers
      • Booleans
        • Truthiness
      • Arrays
  • Unit 3: Control Flow
    • Conditionals (if this, do that)
      • If...Else
        • Logical Operators
      • Switch / Case
      • Ternary Operators
    • Loops (Repeating Code)
      • For...Loop
      • While & Do/While Loops
    • Debugging
  • Unit 4: Functions
    • Functional Programming
    • User Defined Functions
      • Hoisting and Scope
    • Calling a JS Function
  • TL;DR
    • Programming Basics
    • Slideshows & Demos
    • Javascript Syntax Poster
  • Advanced Topics
    • Recursion
    • Structures & Algorithms
    • Mmm... Pi
  • External Links
    • Typing Club!
    • repl.it
    • Khan Academy
    • Geek Reading
    • ECOO CS Contest
Powered by GitBook
On this page
  • Hoisting
  • Scope
  • Let vs Var vs Nothing
  1. Unit 4: Functions
  2. User Defined Functions

Hoisting and Scope

PreviousUser Defined FunctionsNextCalling a JS Function

Last updated 6 years ago

filled with awesome Hoisting and variable declaration goodness. Below is an overview.

Hoisting

What happens if we call a function before it is defined?

Interpreted languages like Javascript work with functions differently than compiled languages like C++. The interpreter looks through the entire code file and decides the 'proper' order of certain code blocks. For example, it prioritizes function declarations over runnable code.

Scope

Variables and functions are declared within other blocks of code (a loop, for example).

Can I use a variable anywhere I want?

Imagine if we never released the memory used by a variable. When a variable is created, memory is allocated for that data. When the block of code where that variable exists has ended, the memory is released (no longer needed). This helps with the efficiency of the program.

The other thing to note is the use of the term let in the example above. In Javascript, there are three ways to create variables (which is quite dangerous). Many people will have their own opinions but it is safest to always declare your variables with let instead of var and certainly never leave out the declaration.

Let vs Var vs Nothing

Why do I need to declare my variables, anyway? Javascript lets me write a = 4 and it still works.

<< More to come >>

Here is a good article