Cheat Sheet: JavaScript

Posted by admin at January 5, 2021

Here is a cheat sheet on the basics of the Javascript language. I will be including conventions E5 to E9 as long they cover the basic markers as indicated in my Cheat Sheet: Bone Collection article where I compare the languages I use.

BOF and EOF
<script> /* start and end the code with these tags... */ </script>
Documentation
/**
  * Function Name
  * A description of the what the function is doing
  * @param nameOfParameter1 data type
  * ...
  * @return data type
  */
Single line comments
// or #
Multi line comments
/* ... */
EOL delimiter
// End of limiter not needed for modern versions of the language
  ;
Basic Output
// basic output with the terminal or browser console
  console.log("BlinkWiki")
  console.warn("BlinkWiki")
  console.error("BlinkWiki")
  
  // output with an alert box
  alert("Hello BlinkWiki!");

  // output with a confirm box
  if (confirm("Are you forgetting something about this language?")) {
     console.log("Here is a cheat sheet!")
  }
DATATYPES
Integer
// numerical value from -2billion to +2billion, with no decimals
  let count;
Float
// numerical value with decimal places
  let count = 20.51;
String
// Collection of characters enclosed in quotes
  let message = "BlinkWiki";
Boolean
// logical variable that has TRUE or FALSE as value
  var isReady = true;
Array
// collection of items that can be referenced by indices
  var words = ["Hello", "BlinkWiki"];
Object
// variable in key-value notation or instance of class
  let blinkWiki = {
      name: "BlinkWiki",
      url: "www.blinkwiki.com",
      pages:36
  }
Undefined
// undefined variables have not been initialised
Null
// null variables do not been any values
  let nullVariable = null;
INITIALIZATION
Initialization keywords
// newer versions / frameworks of the language require let
  let, var
Naming variables
variableName
Naming constants
constantName
Initializing a constant
const PI = 3.14
Initializing an integer
let count = 36
Initializing a string
let message = "Hello BlinkWiki"
OPERATORS
Operators
+ // Addition
  - // Subtraction
  * // Multiplication
  / // Division
  % // Modulus division
  ** // Raised to power
  = Equation
Logical Operators
|| // Logical OR
  && // Logical AND
  == // Logical Equation
  === // Is identical
  != // Logical Inequality
  <> // Logical Inequality
  !=== // Is not identical
  < // Less than
  > // Greater than
  <= // Less than or equal to 
  >= // Greater than or equal to
Assignment Operators
+= // Add and assign
  -= // Subtract and assign
  *= // Multiply and assign
  /= // Divide and assign
  %= // Complete Modulus division and assign
Increments and Decrements
++count // Increase count and then return it
  --count // Decrease count and then return it
  count++ // Return count and then increase it
  count-- // Return count and then decrease it
String Operators
Concatenation
message = "Blink" + "Wiki"; // returns `BlinkWiki`
  message += " Cheat Sheets"; // returns 'BlinkWiki Code Sheets'
CONDITIONALS
If ... ElseIf ... Else
if (condition1) {
    // run this if condition1 was met
  } else if (condition2) {
    // run this if condition2 was met
  } else {
    // run this if neither condition1 nor consition2 were met
  }
Switch ... Case
switch (variable) {
    case "One":
      // variable == "One"
      break;
    case "Two":
      // variable == "Two"
      break;
    case "Three":
      // variable == "Three"
      break;
    default:
      // variable is not "One", "Two" or "Three"
  }
LOOP CONSTRUCTS
While
i = 0;
  // while this condition is still being met
  while (i < 3) {
    // TODO: provide the logic
    i++;
    console.log(i);
  }
Do While
i = 0;
  // start the loop
  do {
    // TODO: provide the logic
    i++;
    console.log(i);
    // stop the loop when the condition below has been met
  } while (i < 3);
Foreach
// define a collection
  collection = array(0,1,2,3);
  // start a loop to go through every item in a collection
  foreach (collection as number) {
    console.log(number);
  }
For
// start the loop from i = 0 and stop when i<3
  for (i=0; i<3, i++) {
    console.log(i);
  }
CONSTRUCTS
Function
function greet() {
    // TODO function logic goes here ...
  }
Function returning a variable
function greet(parameter) {
    // TODO function logic goes here ...
    return "Hello BlinkWiki";
  }
Function with Parameter
function greet(parameter) {
    // TODO function logic goes here ...
  }
Function with Parameter
  and default value
function greet(parameter) {
    // ensure the default value of parameter
    parameter = (parameter === undefined) ? "BlinkWiki" : parameter
    return "Hello " + parameter;
  }
Function with unknown 
  number of parameters
function greet(...parameters) {
    // TODO return the second parameter provided ...
    return parameter[2];
  }
HANDLING ERRORS
Try ... Catch ... Finally
try {
     // TODO: code that throws an exception on condition
  } catch (Exception e) {
     // TODO: inform user of the exception
  } finally {
     // TODO: logic that will run after the exception has been handled
  }
   0 likes

Suggested Read