Javascripting Concepts

Variables

What are Variables?

A variable is a "named storage" that stores a data value beyond a single statement.

Variable Declaration

There a 4 ways to declare a variable in Javascript:

  1. Using var keyword which is used for standard declaration for both global and local variables. This is the most popular way as these type of variables are available in the scope of enclosing function.
  2. Using let keyword which is similar to var however, its scope is not only limited to the enclosing function but, also encloses the block statement.
  3. Using const keyword which makes the variable read-only (or constant). This specific type of variable's value cannot be changed when once it is declared.
  4. Using without a declaritive keyword can be declared as a variable however it is not a recommended method, it can cause errors to your program as it can accidently overwrite your existing variables.

Example

See the Pen Variable Declaration by Towa (@badsmiles) on CodePen.

Variable Scope

Scope in Javascript refers to accessibility and visibility of the variable. There are 3 types of scopes in Javascript:

  • Global Scope: Variables that are declared in a script tag that is outside of a function. These variables in the global scope can be accessed from anywhere in the program.
  • Local Scope / Functional Scope: Variables that are declared inside of a function. These variables can only be accessed within the function and cannot be recalled outside of the code.
  • Block Scope: Variables that are declared within a block "{ }" that use the keyword let. These variables can't be accessed outside of the block.

Functions

What is a Function?

A function (also known as function declaration or function statement) is one of the most fundamental blocks in Javascript as its essentially a block of code or a set of statements that is defined to perform a task. To invoke a function, use the keyword function along with a unique name to define and use braces "{ }" to mark its function block.

Example

See the Pen function by Towa (@badsmiles) on CodePen.

Function Parameter

Parameters in a function are used when defining a function as they are essentially variables listed as part of the function definition. Parameters are separated by commas in the brackets "( )" and can be passed up to 255 parameters.

Example

See the Pen function parameter by Towa (@badsmiles) on CodePen.

Function Return

A return statement ends the function execution and specifies a value that can be returned to the calling statement. Using keyword return will determine and return a single value, variable, expression and end the function.

Conditional Structures

What is a Conditional Statement?

Conditional statements assist with decision makings regarding the flow of execution depending if a block or statements meet the given condition along with possible alternatives. There are 4 types conditional statements:

  1. If Statement: Most common control statement that is executed when a specific code segment of the given condition is "true", if "false" the code block will be skipped.
  2. Else Statement: Specifies the execution for a block of code if the condition is false for cases if all the situations that don't meet the condition.
  3. Else If Statement: Specifies a new condition for testing when the first condition is "false".
  4. Switch Statement (Case Select Statements): Specifies multiple alternative block of codes that will execute if the given cases based on certain condition is met.

Example

See the Pen poJBEmz by Towa (@badsmiles) on CodePen.