JavaScript/CheatSheet: Difference between revisions

From Wiki
Line 125: Line 125:
== Loops ==
== Loops ==
<pre>
<pre>
for
for (var i = 0; i < 10; i++){
while
}
do while
 
while (i < 10) {
  i++;
}
 
do {
  i++;
} while (i < 10);
 
break
break
continue
continue

Revision as of 20:13, 27 December 2021

Data Types

var num = 8;                               // number
var strg = "Joe";                          // string
var vobj = {first: "John", last:"Doe"};    // object
var booli = false;                         // boolean
var arr = ["HTML","CSS","JS"];             // array
var a; typeof a;                           // undefined
var a = null;                              // value null

const pi = 3.14;

typeof strg                                // =string

Arrays

var title = new Array();
title[0] = "Mr.";
title[1] = "Mrs.";
concat()
filter()
forEach()
join()
map()
pop()
push()
reduce()
reverse()
shift()
slice()
splice()
sort()

Strings

charAt()
charCodeAt()
concat()
fromCharCode()
indexOf()
lastIndexOf()
match()
replace() 
search()
slice()
split()
substr()
substring()
toLowerCase()
toUpperCase()
valueOf()

Functions

function addition(number1, number2) {
  return number1 + number2;
}


Operators

+           Addition
-           Subtraction
*           Multiplication
/           Division
(...)       Grouping operator, operations within brackets are executed earlier than those outside
%           Modulus (remainder )
++          Increment numbers
--          Decrement numbers

==          Equal to
===         Equal value and equal type
!=          Not equal
!==         Not equal value or not equal type
>           Greater than
<           Less than
>=          Greater than or equal to
<=          Less than or equal to
?           Ternary operator

&&          Logical and
||          Logical or
!           Logical not

&           AND statement
|           OR statement
~           NOT
^           XOR
<<          Left shift
>>          Right shift
>>>         Zero fill right shift


if - else

if (condition) {
    // if condition is met
} else {
    // if condition is not met
}

switch

switch(var) {
  case 1:
    // code 
    break;
  case 2:
    // code
    break;
  default:
    // code
} 

Loops

for (var i = 0; i < 10; i++){
}

while (i < 10) {
  i++;
}

do {
  i++;
} while (i < 10);

break
continue

Escape Characters

\'      Single quote
\"      Double quote

\\      Backslash
\b      Backspace
\f      Form feed
\n      New line
\r      Carriage return
\t      Horizontal tabulator
\v      Vertical tabulator