Skip to content

Commit 8b13076

Browse files
committed
added content
1 parent 3916ca3 commit 8b13076

8 files changed

+216
-0
lines changed

arithmetic-operators.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
a * (b + c) // grouping
2+
person.age // member
3+
person[age] // member
4+
!(a == b) // logical not
5+
a != b // not equal
6+
typeof a // type (number, object, function...)
7+
x << 2 x >> 3 // minary shifting
8+
a = b // assignment
9+
a == b // equals
10+
a != b // unequal
11+
a === b // strict equal
12+
a !== b // strict unequal
13+
a < b a > b // less and greater than
14+
a <= b a >= b // less or equal, greater or eq
15+
a += b // a = a + b (works with - * %...)
16+
a && b // logical and
17+
a || b // logical or

array-methods.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
var dogs = ["Bulldog", "Beagle", "Labrador"];
2+
var dogs = new Array("Bulldog", "Beagle", "Labrador"); // declaration
3+
4+
alert(dogs[1]); // access value at index, first item being [0]
5+
dogs[0] = "Bull Terier"; // change the first item
6+
7+
for (var i = 0; i < dogs.length; i++) { // parsing with array.length
8+
console.log(dogs[i]);
9+
}
10+
11+
dogs.toString(); // convert to string: results "Bulldog,Beagle,Labrador"
12+
dogs.join(" * "); // join: "Bulldog * Beagle * Labrador"
13+
dogs.pop(); // remove last element
14+
dogs.push("Chihuahua"); // add new element to the end
15+
dogs[dogs.length] = "Chihuahua"; // the same as push
16+
dogs.shift(); // remove first element
17+
dogs.unshift("Chihuahua"); // add new element to the beginning
18+
delete dogs[0]; // change element to undefined (not recommended)
19+
dogs.splice(2, 0, "Pug", "Boxer"); // add elements (where, how many to remove, element list)
20+
var animals = dogs.concat(cats,birds); // join two arrays (dogs followed by cats and birds)
21+
dogs.slice(1,4); // elements from [1] to [4-1]
22+
dogs.sort(); // sort string alphabetically
23+
dogs.reverse(); // sort string in descending order
24+
x.sort(function(a, b){return a - b}); // numeric sort
25+
x.sort(function(a, b){return b - a}); // numeric descending sort
26+
highest = x[0]; // first item in sorted array is the lowest (or highest) value
27+
x.sort(function(a, b){return 0.5 - Math.random()}); // random order sort
28+
29+
/* More Array Methods */
30+
31+
// Accessing Elements I
32+
33+
a1.concat(a2) // Return new array by joining arrays a1 and a2 together
34+
a1.join(separator) // Join all elements of array a1 into a string separated by separator arg
35+
a1.slice(start, end) // Extract a section from start to end of array a1 and return a new array
36+
a1.indexOf(obj) // Return first index of obj within array a1
37+
a1.lastIndexOf(obj) // Return last index of obj within array a1
38+
39+
// Iterating I
40+
41+
a1.forEach(fn) // Calls function fn for each element in the array a1
42+
a1.every(fn) // Return true if every element in array a1 satisfies provided testing function fn
43+
a1.some(fn) // Return true if at least one element in array a1 satisfies provided testing function fn
44+
a1.filter(fn) // Create a new array with all elements of array a1 which pass the filtering function fn
45+
46+
// Iterating II
47+
48+
a1.map(fn) // Create a new array with results of calling function fn on every element in array a1
49+
a1.reduce(fn) // Apply a function fn against an accumulator and each value (from left to right) of the array as to reduce it to a single value
50+
a1.reduceRight(fn) // Apply a function fn against an accumulator and each value (from right to left) of the array as to reduce it to a single value
51+
52+
// Mutating I
53+
54+
a1.pop() // Remove and return last element from array a1
55+
a1.push(obj) // Add obj to end of array a1 and return new length
56+
a1.reverse() // Reverse order of elements of array a1 in place
57+
a1.sort() // Sort the elements of array a1 in place
58+
a1.splice(start, deleteCount, items) // Change content of array a1 by removing existing elements and/or adding new elements
59+
a1.unshift(obj) // Add obj to start of array a1 and return new length
60+
61+
// General I
62+
63+
a1.toString() // Return a string representing array a1 and its elements (same as a1. join(','))
64+
a1.toLocaleString() // Return a localized string representing array a1 and its elements (similar to a1.join(','))
65+
Array.isArray(var) // Return true if var is an array
66+
a1.length // Return length of array a1
67+
a1[0] // Access first element of array a1

data-types.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var age = 18; // number
2+
var name = "Jane"; // string
3+
var name = { first: "Jane", last: "Doe" }; // object
4+
var truth = false; // boolean
5+
var sheets = ["HTML", "CSS", "JS"]; // array
6+
var a;
7+
typeof a; // undefined
8+
var a = null; // value null
9+
10+
false, true; // boolean
11+
18, 3.14, 0b10011, 0xf6, NaN; // number
12+
"flower", "John"; // string
13+
undefined, null, Infinity; // special

if-else.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var age = 23;
2+
var status = "N/A";
3+
4+
if (age < 0) {
5+
// logical condition
6+
status = "Please enter a valid age"; // executed if condition is true
7+
} else if (age >= 0 && age <= 3) {
8+
// else if block comes next
9+
status = "Toddler";
10+
} else if (age >= 4 && age <= 12) {
11+
status = "Kid";
12+
} else if (age >= 13 && age <= 17) {
13+
status = "Teenager";
14+
} else if (age >= 18 && age <= 39) {
15+
status = "Young Adult"
16+
} else if (age >= 40 && age <= 59) {
17+
status = "Middle Aged Adult";
18+
} else (age >= 60) {
19+
// Executed if all other conditions fail
20+
status = "Old and Senior Citizen"
21+
}

js-global-functions.js

Whitespace-only changes.

string-methods.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var abc = "abcdefghijklmnopqrstuvwxyz";
2+
var esc = 'I don\'t \n know'; // \n new line
3+
var len = abc.length; // string length
4+
abc.indexOf("lmno"); // find substring, -1 if doesn't contain
5+
abc.lastIndexOf("lmno"); // last occurance
6+
abc.slice(3, 6); // cuts out "def", negative values count from behind
7+
abc.replace("abc","123"); // find and replace, takes regular expressions
8+
abc.toUpperCase(); // convert to upper case
9+
abc.toLowerCase(); // convert to lower case
10+
abc.concat(" ", str2); // abc + " " + str2
11+
abc.charAt(2); // character at index: "c"
12+
abc[2]; // unsafe, abc[2] = "C" doesn't work
13+
abc.charCodeAt(2); // character code at index: "c" -> 99
14+
abc.split(","); // splitting a string on commas gives an array
15+
abc.split(""); // splitting on characters
16+
128.toString(16); // number to hex(16), octal (8) or binary (2)
17+
18+
/* More Strings Methods */
19+
20+
var str = "Hello World";
21+
var str1 = "United States of America";
22+
var str2 = "states";
23+
24+
// Basics I
25+
26+
str.length // Return length of string str
27+
str[n] // Return nth character of string str
28+
str.charAt(index) // Return character in string str at specified index
29+
str.toLowerCase() // Convert string str to lower case
30+
str.toUpperCase() // Convert string str to upper case
31+
32+
// General I
33+
34+
str.indexOf(substr) // Return first index within string str of substring substr
35+
str.split(separator) // Split string str into an array of substrings separated by param separator
36+
str.trim() // Trim whitespace from beginning and end of string str
37+
str1 < str2 // Return true if str1 is less than str2
38+
str1 > str2 // return true if str1 is greater than str2
39+
40+
// Experimental I
41+
42+
str.codePointAt(index) // Return non-negative int from string str that is the UTF-16 encoded code point at given index
43+
str1.includes(str2) // Return true if str2 is found in string str1
44+
str1.endsWith(str2) // Return true if string str1 ends with string str2
45+
str.normalize() // Return Unicode Normalization Form of string str
46+
str.repeat(int) // Return string repeated int times of string str
47+
str1.startsWith(str2) // Return true if string str1 starts with str2
48+
str[@@iterator]() // Return a new Iterator that iterates over the code points of string str, returning each code point as String value
49+
50+
// General II
51+
52+
str.charCodeAt(index) // Return number indicating Unicode value of char at given index of string str
53+
str1.concat(str2) // Combine text of strings str1 and str2 and return a new string
54+
str.lastIndexOf(substr) // Return last index within string str of substring substr
55+
str.slice(start, end) // Extract a section of string str from start to end
56+
str.substr(start, length) // Return characters in string str from start having length length
57+
58+
// General III
59+
60+
str.substring(index1, index2) // Return subset of string str between index1 and index2
61+
str.toLocaleLowerCase() //Convert chars in string str to lower case while respecting current locale
62+
str.toLocaleUpperCase() // Convert chars in string str to upper case while respecting current locale
63+
str.trimLeft() // Trim whitespace from left side of string st
64+
str.trimRight() // Trim whitespace form right side of string str
65+
66+
// General IV
67+
68+
str1.localeCompare(str2) // Return -1, 0, or 1 indicating if string str1 is less than, equal to, or greater than str2
69+
str.match(regexp) // Match a regular expression regexp against string str
70+
str1.replace(regexp, str2) // Replace matched regexp elements in string str1 with string str2
71+
str.search(regexp) // Return position of search for a match between regexp and string str

switch-case.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
switch (
2+
new Date().getDay() // input is current day
3+
) {
4+
case 6: // if (day == 6)
5+
text = "Saturday";
6+
break;
7+
case 0: // if (day == 0)
8+
text = "Sunday";
9+
break;
10+
default:
11+
// else...
12+
text = "Weekday";
13+
}

variables.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var a; // variable
2+
var b = "init"; // string
3+
var c = "Hi" + " " + "Joe"; // = "Hi Joe"
4+
var d = 1 + 2 + "3"; // = "33"
5+
var e = [2, 3, 5, 8]; // array
6+
var f = false; // boolean
7+
var g = /()/; // RegEx
8+
var h = function() {}; // function object
9+
10+
const PI = 3.14; // constant
11+
var a = 1,
12+
b = 2,
13+
c = a + b; // one line
14+
let z = "zzz"; // block scope local variable

0 commit comments

Comments
 (0)