Notes

Edit on GitHub
JavaScript
5 minutes

JS Notes

7 JS Data Types:

  • undefined
  • null
  • boolean
  • string
  • symbol
  • number = numeric data
  • object

If you do a mathematical operation on an undefined variable your result will be NaN which means Not a Number. If you concatenate a string with an undefined variable, you will get a literal string of undefined.

Compound Assignment With Augmented Multiplication is just *=

CodeOutput
\'single quote
\"double quote
\\backslash
\nnewline
\rcarriage return
\ttab
\bbackspace
\fform feed

Arrays []

[' '] is an empty array, { } is not. the individual characters of a string literal cannot be changed. On the other hand, the entries of arrays are mutable and can be changed freely.

.push() adds data to the end of an array .pop() pops a value off the end of an array .shift() removes the first element of an arary .unshift() adds elements to the beginning of an array

Parameters are placeholders for values that are to be input to a function when it is called. Arguments are the actual values that are passed.

Scope

Scope refers tp teh visibility of variables. Variables which are defined outside a function are called Global scope. Variables which are used without the var keyword are automatically created in global scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with var.

Variables which are declared within a function, as well as the function parameters have local scope. That means, they are only visible within that function.

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.

In order for JavaScript to compare two different data types (for example, numbers and strings), it must convert one type to another.

== is the equality operator === is the strict equality operator, it test both data type and the value of the compared element.

Switch statements

 1switch(val) {
 2	case val1:
 3		// statement 1;
 4		break;
 5	case val2:
 6		// statement 2;
 7		break;
 8	...
 9	case valn:
10		// statement N;
11		break;
12	default:
13		// default statement;
14}

Switch statement with multiple identical cases

1switch(val) {
2  case 1:
3  case 2:
4  case 3:
5    // statement
6    break;
7} 

Objects

calling properties

  • dot notation testObj.name
  • bracket notation testObj[“name”]

bracket notation

is good for calling properties that have space in their names. Or if the property name is a number It is also good for using a variable to access a property (read: you must use bracket notation when accessing a property using a variable).

  • testObj[16]
  • testObj[“my name”]
  • testObj[name] – here name is a variable, you can tell by the lack of ' '

modifying properties

adding and editing are done the same way: testObj.name = "Whatever"1
deleting is done using the delete keyword: delete testObj.name

checking if a property exists

we use the .hasOwnProperty() method on the object. e.g: Obj.hasOwnProperty("propname");. It returns true or false

accessing sub-properties

the sub-properties of an object can be accesssed by chaining together the dot or bracket notation. e.g: myStorage.car.inside["glove box"]

#Arrays vs Objects

  • Arrays are index based, you access properties like pet[3] to get the 4th pet value.
  • Objects use dot and bracket notations, and have key-value pairs.

Loops

For

 1// for ([initialization]; [condition]; [final-expression])
 2for (i = 0; i < 5; i++ ) {
 3};
 4for (i = 0; i < 10; i+=2) {
 5	// will iterate EVEN numbers because i starts at 0
 6};
 7for (i = 1; i < 10; i+=2) {
 8	// wil iterate ODD numbers because of i starting at 1
 9};
10for (i = 10, i > 0; i-=2) {
11	// will iterate EVEN numbers backwards because of `i-=2` 
12}

For loop to iterate over arrays

Remember that Arrays have zero-based numbering, which means the last index of the array is length -1. Our condition for this loop is i < arr.length, which stops when i is at length -1.

1for (i = 0; i< arr.lenth; i++) {
2	// do something
3}

nested For loop to iterate over nested sub-arrays

1  var product = 1;
2
3  for(i = 0; i < arr.length; i++) {
4    for(j = 0; j < arr[i].length; j++) {
5      product *= arr[i][j];
6    }
7  }

While Loops

1var i = 0;
2while (i < 5) {
3  myArray.push(i);
4  i++;
5}

Generating numbers

Math.random() generates a ranmdom number between 0 and 1 Math.floor() rounds down to the nearest whole number

Regex

You can invert any match by using teh UPPERCASE version of the selector, e.g: \S instead of \s will match anything that isn’t a whitespace.

/ is the start/end g global, return all matches, not just the first one i ignore case, case-insensitive \d digit (0-9) + match one or more, e.g: /\d+/g \s find spaces. e.g: /\s+/g " " space \r carriage return \n new line \t tab \f form feed

Object vs. Array

JavaScript has only one data type which can contain multiple values: Object. An Array is a special form of object.

  • Arrays are created with []. Objects are created with {}

  • Arrays use index value to access array items (veggie[6]). Objects use a key to access their properties (person['name'] or person.name).

  • Math.random() generates a random number b/w 0 and 1.

  • Math.floor() rounds the number down to it’s nearest whole number