This is a quick reference for Javascript. Kuhikuhi Wikiwiki is an Hawaiian Equivalent of Quick Reference. (Best viewed on a laptop.)
The Operators
Math: +, =, *, /, ** &y_arr; plus, minus, times (multiply), divided by, exponent (to the power of)
Comparison: <, <=, ==, === >=, > &y_arr; less than, less than or equal to, is equal to, is equal to in both type and value, greater than or equal to, greater than
Math: x % y &y_arr; return remainder of x / y
++, -- &y_arr; increment, decrement
+=, -=, *=, /=, %=/ **= &y_arr; x += y results in x = x + y. Similar for other operators.
Concatenation: (happens when x or y are a string | text) +, += &y_arr; x + y | concat(x,y), x = x + y | x = concat(x,y)
Comparison: !, &&, || &y_arr; not, and, or
If: x = (y == z) ? a : b &y_arr; if (y == z){x = a} else {x = b}
Function: abc = (x,y) => x * y &y_arr; is the same as the function definition abc = (x, y){return x * y}, is the same as abc = qrs(x, y){return x * y}
&y_arr; is also similar to the function expression function abc(x, y){return x * y} Function expressions must be defined before they are called. Best place to do that is usually within <head>...&;lt;/head> of the html webpage.
Main Commands, Etc.
All commands can be ended by a semicolon(;) or a command on the next line.
Outer quotation marks can be either single or double. They might also be ` for
template literals.
Many math functions are available through
math.js just proceed the function with math. Example: math.max(...)
Comment: Comments are not commands. They don&';t do anything. They&';re for informations purposes only.
// starts a comment. Anything on that line including and after // is a comment
Comments can be enclosed in /*
comment */
Defining Variables, Etc.
Variable names can contain letters, numbers, _, $. They cannot begin with a number. Everything, including variable names in Javascript is case sensitive.
&l";Global&r"; Variables are defined when they are first assigned a value
&l";Local&r"; Variables are defined as: var variableA. Local variables basically exist throughout and only in a function. They are available and can be changed by any function that function calls / executes.
&l";Constants&r"; are defined as: const constantA=value; You cannot assign a new value to a constant. However, if the constant is an array or object, I can change the value of something in that array or object. I don&';t use const. I might change my mind later about that thing being constant.
&l";Block&r"; Variables are only available to the current block of code. Use the let statement (similar to var). But, what&';s a block of code? Go to this
website and scroll to
Definition for the answer. There are so many variations that I don&';t use let. One of the most common examples of let is for defining i within a for command. But, I might need to know the value of i after the for command ends. And if I define it like this, it doesn&';t exist after that for command.
Group variables include arrays, objects.
Logicif(condition){...}
else{...}
switch(variableA){
case 'value1':
...
...break //break is optional
...
default: ...
}
Assigning ValuesValues are typically assigned using a=b The value of b is assigned to a. Arrays and Array elements can be assinged this way. There&';s also the push command.
Loops:
for(i=0;i<variableA.length;i++){...}
//variableA is often an array
i=0;while(i<variableA.length){...;i+=1}
i=0;do{...}while(i<variableA.length)
More options:
Medium Article by Mason Aviles
Define and use Functions:
Define Function: function functiona(parameter1, parameter2, ...){...}
Use Function: functiona(parameter1, parameter2, ...)
resulta=functiona(parameter1, parameter2, ...)
2D Arrays
I think of 2D arrays as having the same number of rows in every column. But, arrays in javascript are really lists. So, rows can have varying numbers of columns.
1D arrays can be defined and initializeds as: var my1Darray=[]
2D arrays can be defined and initializeds as: var my2Darray=[][]
Many programming languages would specify the indexes of a 2D array as my2Darray(i,j). Javascript specifies them as my2Darray[i][j]
Javascript is a zero-based language. So, array indexes start at zero and go to my1Darray.length or my2Darray[i].length for the length of this row.
Arrays, rows, and columns can contain arrays.
Regex
Consider using string.includes(substring) before using regex. It&';s simpler and possibly quicker.
mdn reference