Variables, Functions & String Methods
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, andnull
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:
constdeclares a block-scoped variable that cannot be reassigned. This should be your go-to variable declaration.letdeclares a re-assignable block-scoped variable. Use only when reassignment is necessary.vardeclares a re-assignable, hoisted, function-scoped variable. Do not usevar. Hoisting and function-scoped variables can cause unexpected behavior (read more below).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
varHoisting is a feature of JavaScript that allows certain variables/functions to be referenced before they are declared.
Variables declared with the
varkeyword and Functions declared with thefunctionkeyword are hoisted.Variables declared with
constorletand Arrow Functions are NOT hoisted.
Variables declared with
varwill be given the valueundefineduntil their assignment statement is reached.Functions declared with
functionare fully invoke-able anywhere within their scope.
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
returnstatement, you can omit thereturnkeyword 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
consoleandMathand 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,
undefinedwill be returned:
String Length
Every string also has a
lengthproperty 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