Variables, Functions & String Methods

Follow along with code examples here!

Slides

Data Types

  • All values have a type. The data type determines what you can do with a given value.

  • There are five main primitive data types: Number, String, Boolean, undefined, and null

  • There are also three reference data types: Objects, Arrays, and Functions (we'll talk about these later on).

    • Technically, there is only one reference data type: Objects. Functions and Arrays are subsets of Objects

Variables

  • A Variable in JavaScript is a named container for data.

  • There are 4 ways to declare a new variable, but only the first two of should be used:

    1. const declares a block-scoped variable that cannot be reassigned. This should be your go-to variable declaration.

    2. let declares a re-assignable block-scoped variable. Use only when reassignment is necessary.

    3. var declares a re-assignable, hoisted, function-scoped variable. Do not use var. Hoisting and function-scoped variables can cause unexpected behavior (read more below).

    4. Global variables are declared without a keyword. Do not use global variables.

  • We can reference a variable by name to either reassign its value or to access its current value:

Hoisting: Why We Don't Use var

TLDR: Don't use var or function. Do not reference a variable or function before it has been declared.

  • Hoisting is a feature of JavaScript that allows certain variables/functions to be referenced before they are declared.

    • Variables declared with the var keyword and Functions declared with the function keyword are hoisted.

    • Variables declared with const or let and Arrow Functions are NOT hoisted.

  • Variables declared with var will be given the value undefined until their assignment statement is reached.

  • Functions declared with function are fully invoke-able anywhere within their scope.

Hoisting (like most weird JS things) stems from the fact that when JS was invented eons ago, there was a desire that JS should never crash a site. Ever. So bugs that would normally get caught in development would sneak through so the page wouldn’t crash, but it also wouldn’t work properly. Nowadays, we prefer to find these errors in development, so crashing is ok.

Functions

  • A Function is a named container for statements that can be invoked to execute its statements.

Arrow Functions

  • There are many ways to create a function, we will use Arrow Function syntax:

Implicit Returns

There are a couple of ways we can simplify our Arrow functions:

  • If the function's body is just one line, you can omit the {}

  • If the function's body is just a return statement, you can omit the return keyword too. This is called an implicit return

Function Calls, Parameters, Arguments, and Return Statements

  • A function call statement changes the control flow by "activating" the function. Calling a function sets the first line of code in the function as the next line of code to be executed.

  • Parameters are variables created when a function is invoked that reference the inputs (a.k.a. arguments) provided in the function call.

  • A return statement terminates a function and returns a value to the location of the function call.

Scope

  • Scope refers to where in our code variables can be referenced. Files, functions, and code blocks {} each create a new scope.

  • There can be any number of scopes in a single file but we will describe them as either:

    • Global Scope β€” Variables that are accessible across the entire file. Includes things like console and Math and variables/functions declared at the outermost scope of the file.

    • Local Scope β€” Variables that are accessible only in the current function or code block.

  • "Outer scopes" cannot see variables in "inner scopes".

  • "Inner scopes" can see variables in "outer scopes".

  • Parameters are treated as locally scoped variables.

Zooming in on Strings

String Indexes and Bracket Notation

  • A string is a series of characters surrounded by "double quotes", 'single quotes' or `backticks`

  • Each character in a string, including spaces, has an index β€” a numbered position starting at 0.

  • Bracket Notation is used to read a single character in a string by its index: string[index]

  • If you use bracket notation and there is no character at the given index, undefined will be returned:

String Length

  • Every string also has a length property that is accessed using dot notation: string.length

Strings are Immutable

  • Strings are "immutable" (unable to be mutated). In other words, you cannot use bracket notation to change characters of a string.

  • If you try, no error will be thrown, nothing will happen.

String Methods

  • A method is a function that is attached to a value. Often, methods are used to manipulate the value they are attached to.

  • Methods are invoked using dot notation: value.method()

  • First, let's look at some "read only" string methods: includes, startsWith, endsWith, indexOf, lastIndexOf

  • The following methods make a copy of a string before performing some modifications: slice, toUpperCase, toLowerCase, repeat, replace, replaceAll

Last updated