- Javascript is named the way it is because it was a marketing ploy using the Java name
- Javascript was a scripting language that was created to compete with applets
- Ecmascript is the official name for Javascript
- Works with REST API for data layer, serverless technology, etc
- Javascript can help you build and do everything
- Hoisting is when you can call a function or a variable before it is declared example: var hoist; console.log(hoist); hoist = 'The variable has been hoisted.";
- var is for global variables that will change
- let is for global variables that will chance
- const is for global variables that will NOT change. const is good for security purposes and sometimes required. Const is for global variables that will NOT change
- example for let:
function whatIsLet(sum) {
let whatIsThis = ' This can be changed is within lexical - or local - scope.';
return sum + 1;
console.log(whatIsThis);
}
- string represents textual data 'hello', "hello world!" etc
- number is an integer or a floating-point number 3, 3.234, 3e-2 etc.
- bigInt is an integer with arbitrary precision 900719925124740999n , 1n etc.
- boolean is any of two values: true or false
- undefined is a data type whose variable is not initialized. An example is:
let a;
- null denotes a null value
let a = null
- symbol data type whose instances are unique and immutable let value = Symbol('hello');
- object consists of key-value pairs of collection of data
let student - {};
- NAN is a property of a global object (kind of like the planet Pluto because some consider it a data type and some do not);
- if this, then do this
- format is:
if (name === "John) {
console.log("red")
} else if (name === "Aaron") {
console.log("blue")
} else {
console.log("green")
}
The format of it is below:
// switch
const fruit = 'Pears';
switch (fruit) {
case 'Oranges':
console.log('Oranges are $0.59 a pound.');
break;
case 'Mangoes':
case 'Papayas':
console.log('Mangoes and papayas are $2.79 a pound.');
// expected output: "Mangoes and papayas are $2.79 a pound."
break;
default:
console.log(`Sorry, we are out of ${fruit}.`);
}
- one equal sign is assigning something to a variable
- two equals signs is comparing something but not data types
- explicit equals is === It also compares data types
- Example:
var newArray = [1, 2, 3, 4];
/*an array is enclosed by brackets and each value is separated by commas. Arrays are declared
with a variable by using let, const, or var depending on whether the items in the array with be
changed or not.*/
- Arrays start at [0] index
- Example:
var numbers = [1, 2, 3, 5, 8]; //array of numbers
var cars = ['Mazda', 'Honda', 'Chevy', 'Ford']; //array of strings
var honda = cars[3];
/*assigning a new variable with the value from the cars array. honda will be equal to the string
"Ford" because the array starts at [0] index*/
- There are a number of array methods built into Javascript. Here are a few of them:
arr.push(...items) – adds items to the end,
arr.pop() – extracts an item from the end,
arr.shift() – extracts an item from the beginning,
arr.unshift(...items) – adds items to the beginning.
arr.splice(start[, deleteCount, elem1, ..., elemN])
/*It modifies arr starting from the index start: removes deleteCount elements and then inserts elem1,
..., elemN at their place. Returns the array of removed elements.*/
//Example of splice
let arr = ["I", "study", "JavaScript"];
arr.splice(1, 1); // from index 1 remove 1 element
console.log( arr ); // ["I", "JavaScript"]
- Loops are like interstate loops. It takes you around and around until you reach your desired exit point It is a piece of logic that dictates how many times to run something until you reach the designated end point
- for loop is the easiest form of a loop. Very close to an "if" statement but it has more options.
- Example:
for(condition; end condition; change){
// do it, do it now
}
- Another example:
for (var integer = 0; integer < 10; integer = integer++) {
console.log('yay!');
}
- Used when you need to break logic. It doesn't increment.
- Example:
var i = 0,
x = '';
while (i < 5) {
x = x + 'The number is ' + i;
i++;
}
- Example
var i = 0;
do {
console.log(i + ' ');
i++; // incrementing i by 1
} while (i < 100);
- Example
const array1 = ['a', 'b', 'c'];
for (const element of array1) {
console.log(element);
}
- break; //stops the loop
- continue; //skips this iteration and goes to the next iteration