strict mode?
it helps us avoid future bugs that JS might not catch such as typos.
add as string on first line of code before writing any code!
always add!
Functions
we can create reusable chunks of code using functions : keeping code dry!!
function works as a skeleton for a process, like a machine that does a certain job!
but this machine cannot work until the process is called, like pressing a button . And in order to get the finished product you must always ask for a return. If we don't ask for a return the process will run but it won't give any results!
once a return is declared it will exit out of a function, so be careful of prematurely calling it and exiting the code when there are still things to process.
Function declarations and expressions
function declaration is where you name a function straight away, in this case: calcAge1.
However a function expression is where you store an anonymous function (function without name) in a variable.
difference between two is declarations can be called before being declared however with expression we can't do this due to 'hoisting'. However for clean code always use expression!
Arrow functions
a simpler way to write functions, however it can't replace traditional functions completely as you can't use 'this' keyword in arrow functions
Functions calling other functions
a function that is declared outside can be called in another function. This is important when there are a lot of components and for dry coding.
Arrays
type of data structure
Arrays are big containers that stores variables and later reference them
- two ways to write arrays
literal syntax (more common):const friends = ["michael", "Jonas", "Theo"]
or
- arrays are zero based (count starts at 0, 1,2..)
.length = counts number of elements in array
ex: friends.length = 3
inside the bracket, we can put in any expression : console.log(friends[friends.length - 1]);
- we can replace any element in the array
- In arrays even if the variable is set as const, you can still mutate the array, this is because its not a primitive value!!
However we can't replace the entire array, friends =['Alison' , 'Ray']
- We can place variables, numbers, strings and even other arrays into an Array
Array methods and operations
methods to use for arrays
- push : adds an element to an array at the end, it is a methood but also a function
since push is also a function, it can also return a value. to capture an array after adding or subtracting an element.
just store it in a variable
- unshift: adds element to beginning of array
- pop: removes element at the end of an array
- shift: removes element from the start of an array
- indexOf: gets the index value of an element
if we search for an element that isn't there we will get -1
- includes(ES6 grammar): gives boolean value if element is in the array = strict equality (string and math is differenciated)
we can use includes to write conditionals
Objects
stored in curly braces { }
objects are consisted of key and values.
each key is also called a property: so object called roxy has 5 properties.
difference between object and array is that the order of an object doesn't matter at all when we want to retrieve value from an object.
array =ordered data
object = unstructured data
to retrive data from an object there are two methods:
1. dot notation
2. bracket notation
brackets notations are useful when storing a result in a variable and using said variable to search a certain value within an object as dot notation can only fetch back info if we use the correct key.
For example:
so we do this instead!!
we can also add more keys and values to the object using both dot and bracket notations
object methods
we can store functions in objects as a key and value form because functions count as values!
like this:
we call calcAge a method!
to use brackets to get result:
HOWEVER since 1999 is already stored in key birthYear we can source this directly by using this.
this is equal to the object calling the method!!
for example:
here this is referencing the object roxy
The for Loop
- a control structure like if, else
Loop help us automate repetitve tasks!
a loop statement has 3 parts!
1.) counter called rep
2.) logical condition evaluated before each iteration of the loop, if the condition is true the next rep will run
3.) counter increaser
for loops keep running while condition is true!
using the for loop with an array
we can also create an empty array and fill it using the for loop
however a cleaner version of the above will be
continue and break in for loops
- we use continue when we want to skip some parts of the loop
for example, if we only want to get results for string, we can write an if statement in the for loop
- break terminates the entire loop (similar to break in switch statement)
so the loop will only print twice, Roxy and Lee before terminating at 2024-1999 (25)
looping backwards and loop within a loop
- looping backwards
we can also print out an array in the opposite direction by doing this:
- loop inside a loop
-
for (let excercise = 1; excercise <= 3; excercise++) {console.log(`starting excercise number ${excercise}`);
for (let rep = 1; rep <= 5; rep++) {console.log(` excercise ${excercise} lifting weight repetion ${rep}`);
for (let breath = 1; breath <= 1; breath++) {console.log(`breathe in, breathe out`);}}}
'Javascript' 카테고리의 다른 글
Data structures, Moden operators and strings (0) | 2024.04.23 |
---|---|
JavaScript: Behind the scenes (0) | 2024.04.04 |
DOM and Events Fundamentals (0) | 2024.04.03 |
JavaScript fundamental notes 1 (0) | 2024.01.23 |