Week 3 - Functions and strings

Functions

String functions

JavaScript Modules

Scope

IDE navigation

DRY (Don’t Repeat Yourself)

Function size

Using LLMs for efficient learning

Practice

Assignment

Back to core program

What is scope?

With the introduction of functions and modules it is time to go into a concept we call scope. Scope in JavaScript determines where variables, functions, and objects are accessible in your code. There are four main types of scope: Global scope makes variables accessible throughout the entire program, variables declared outside any function or block are in the global scope. Module scope applies when using ES6 modules; variables declared at the top level of a module are only accessible within that module unless explicitly exported, helping prevent global namespace pollution. Function scope means variables declared inside a function (using var, let, or const) are only accessible within that function and cannot be referenced from outside. Block scope, introduced with let and const, restricts variable access to the block (denoted by curly braces {}) in which they are declared, such as within if statements, loops, or any code block.

That is a lot of information, so watch this video that goes over all of this with examples:

https://www.youtube.com/watch?v=TkFN6e9ZDMw

<aside> ⌨️

Hands on: In the video, he runs it in the browser. Let’s double check that the same behaviour happens in Node.js. Create a new folder on your computer and recreate the code in the video

</aside>

<aside> ❗

As mentioned in the video, the var keyword is old and does not work in the same way as const and let. It is a best practice to avoid var as much as possible

</aside>


The HackYourFuture curriculum is licensed under CC BY-NC-SA 4.0 **

CC BY-NC-SA 4.0 Icons

*https://hackyourfuture.net/*