JavaScript, finally!

Monajaved
3 min readMar 31, 2021

--

The more you get into programming, the more you see or read javaScript code. It’s one of the most widely used languages out there and rightfully so! It really does have so much to it that I feel like I probably learnt only 20% of it in my phase 4 of Flatiron School.

Let’s cut straight to what I found most interesting.

fetch();
I had always seen fetch being used and I only had a vague idea of what it is prior to making my application.
We need to connect our backend (I used Rails) with our frontend.
This is where I used or learnt more about fetch.
A Fetch Request retrieves data from our backend(API) and returns it back to us in JSON format. Upon making a Fetch Request , we get some data upon which we call the json() function which then returns a promise object.
Since javaScript runs asynchronously we can ask JavaScript the order to execute the fetch request by adding .then() functions so after our promise is done pending we can then move on to grabbing our data from said request.

Arrow functions
Difference between regular functions and arrow functions are the way they bind objects to which they are called upon. An arrow function automatically binds the object that called it rather than having to explicitly use the word bind(this). Arrow function expressions are best suited for non-method functions whereas standard functions are best suited for object methods.

var, let and const
After the introduction to ES2015, using ‘var’ is the weakest available method of defining a variable. With var, the variable may or may not be reassigned, and the variable may or may not be used for an entire function.
let’ and ‘var’ are destructive. We never use var and on rare occasions we use let.
const’ is non-destructive, and generally preferred, you cannot change the value or the constant.

callback functions
A callback function is a function within an argument of another function to be called upon later in the code. Good examples are filter(),map(),and find().

hoisting
Hoisting is the action of variable or function declaration being moved to the top of their scope regardless if its global or local.

constructor functions
The constructor function lies within a Class. We use constructors to instantiate our objects with attributes with the use of the ‘this’ keyword.

forEach
forEach’ is used when you have an array and you need to manipulate each element inside that array.

map
map’ is used when you need to manipulate an array and store the new values into another array non destructively.

filter
filter’ is used when you need to narrow down objects within a collection.

find
find’ is used when you want to find an object within a collection.

reduce
reduce’ is used when you need to reduce objects within an array.

preventDefault
Keeps the page from refreshing when AJAX calls are made.
Stops default action from happening.
The default behavior of a form, for example is to try and submit the form data based on a defined action causing a redirect. We don’t need to define an action. The result, however, is that the form redirects to the current page, causing a refresh. By using event.preventDefault(), we stop this behavior from happening.

So that is it for now, will probably keep coming back here to update interesting learnings of javaScript because I am so not done!

--

--

No responses yet