Javascript

JavaScript provides seven different data types which are undefined, null, boolean, string, symbol, number, and object.

% remainder

One such operator is the += operator. myVar += 5 myVar = myVar + 5;

In JavaScript, you can escape a quote from considering it as an end of string quote by placing a backslash () in front of the quote. var sampleStr = "Alan said, \"Peter is learning JavaScript\"."; 'This string has "double quotes" in it. And "probably" lots of them.'

Here is a table of common escape sequences: Array Unlike strings, the entries of arrays are mutable and can be changed freely. .push() add. unshift() works exactly like .push(), but instead of adding the element at the end of the array, unshift() adds the element at the beginning of the array. .pop() is used to "pop" a value off of the end of an array. We can store this "popped off" value by assigning it to a variable. .shift() comes in. It works just like .pop(), except it removes the first element instead of the last. push and pop unshift and shift

Scope It is possible to have both local and global variables with the same name. When you do this, the local variable takes precedence over the global variable.

== vs === Strict equality (===) is the counterpart to the equality operator (==). Unlike the equality operator, strict equality tests both the data type and value of the compared elements. 3 === 3 // true 3 === '3' // false The inequality operator (!=) is the opposite of the equality operator. It means "Not Equal" and returns false where equality would return true and vice versa. Like the equality operator, the inequality operator will convert data types of values while comparing. Examples 1 != 2 // true 1 != "1" // false 1 != '1' // false 1 != true // false 0 != false // false

Condition if () { } else if () { } else { } switch (num) { case value1: statement1; break; case value2: statement2; break; ... case valueN: statementN; break; default: statement; } case values are tested with strict equality (===). The break tells JavaScript to stop executing statements. If the break is omitted, the next statement will be executed.

If the break statement is omitted from a switch statement's case, the following case statement(s) are executed until a break is encountered. If you have multiple inputs with the same output, you can represent them in a switch statement like this: switch(val) { case 1: case 2: case 3: result = "1, 2, or 3"; break; case 4: result = "4 alone"; } If you have many options to choose from, a switch statement can be easier to write than many chained if/else if statements. The following: if (val === 1) { answer = "a"; } else if (val === 2) { answer = "b"; } else { answer = "c"; } can be replaced with: switch (val) { case 1: answer = "a"; break; case 2: answer = "b"; break; default: answer = "c"; }

A common anti-pattern is to use an if/else statement to do a comparison and then return true/false: function isEqual(a,b) { if (a === b) { return true; } else { return false; } } Since === returns true or false, we can return the result of the comparison: function isEqual(a,b) { return a === b; }

Object "." and [] The second way to access the properties of an object is bracket notation ([]). If the property of the object you are trying to access has a space in it, you will need to use bracket notation. Note that property names with spaces in them must be in quotes (single or double). myObj["Space Name"];

array You will need to place a comma after every object in the array, unless it is the last object in the array. use . [] [1]

conditions ? statementA : statementB ; like if else

Code: for (var x = 0; x < contacts.length; x++){ if (contacts[x].firstName === firstName) { if (contacts[x].hasOwnProperty(prop)) { return contacts[x][prop]; } else { return "No such property"; } } } return "No such contact";

Random: Math.floor(Math.random() 20); floor: nearest whole number, random: (0-1),not equal 1 [myMax, myMin] Math.floor(Math.random() (myMax - myMin + 1)) + myMin

Regular Expressions:

  1. /the/gi gi: global,all the match word
  2. var Count = testString.match(expression).length
  3. One such selector is the digit selector \d which is used to retrieve one digit (e.g. numbers 0 to 9) in a string.
  4. In JavaScript, it is used like this: /\d/g. Appending a plus sign (+) after the selector, e.g. /\d+/g, allows this regular expression to match one or more digits. \s white space \r (the carriage return), \n (newline), \t (tab), and \f (the form feed).
  5. You can invert any match by using the uppercase version of the regular expression selector. For example, \s will match any whitespace, and \S will match anything that isn't whitespace.

OOP 1.A constructor function is given a capitalized name to make it clear that it is a constructor. Here's an example of a constructor function: var Car = function() { this.wheels = 4; this.engines = 1; this.seats = 5; }; var myCar = new Car(); myCar.nickname = "Jack";

  1. Make Unique Objects by Passing Parameters to our Constructo var Car = function(wheels, seats, engines) { this.wheels = wheels; this.seats = seats; this.engines = engines; }; We can also create private properties and private methods, which aren't accessible from outside the object. To do this, we create the variable inside the constructor using the var keyword we're familiar with, instead of creating it as a property of this.

Map The map method is a convenient way to iterate through arrays. Here's an example usage:

var oldArray = [1, 2, 3]; var timesFour = oldArray.map(function(val){ return val * 4; }); console.log(timesFour); // returns [4, 8, 12] console.log(oldArray); // returns [1, 2, 3]

Reduce var singleVal = array.reduce(function(previousVal, currentVal) { return previousVal - currentVal; }, 0); 0 is the initialValue

Filter The filter() method creates a new array with all elements that pass the test implemented by the provided function.

Sort sort can be passed a compare function as a callback. The compare function should return a negative number if a should be before b, a positive number if a should be after b, or 0 if they are equal. Here is an example of using sort with a compare function that will sort the elements from smallest to largest number:

var array = [1, 12, 21, 2]; array.sort(function(a, b) { return a - b; });

Reverse reverse is another array method that alters the array in place, but it also returns the reversed array. var myArray = [1, 2, 3]; myArray.reverse();

Concatenate Here is an example of concat being used to concatenate otherArray onto the end of oldArray: newArray = oldArray.concat(otherArray);

Split Here is an example of split being used to split a string at every s character: var array = string.split('s');

Join We can use the join method to join each element of an array into a string separated by whatever delimiter you provide as an argument.

results matching ""

    No results matching ""