Beginner Javascript: Browser (Chrome) Shortcuts
Beginner Javascript: Browser (Chrome) Shortcuts
Beginner JavaScript
Last Edited Aug 29, 2020 3:23 AM
Add a comment…
Running JavaScript:
• We can run JS on the browser's console by which we will have access to the
page we are on.
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 1/32
8/29/2020 Beginner JavaScript
Always try to have your script tag before the closing body tag, because we can
access the HTML elements written above the script tag as they are above the JS.
• Also, we can run JS in another file using script tag with src attribute:
• Also we can run it via Node.js - instead of running in the context of a website we
run it in an actual machine. In console, run: node file.js
Declaring a variable:
var first = 'Soumya';
var x = 'hey'; y = 'hi'; let cool = true; cool = false; const age = 10;
age = 11; // wrong: throws error
JavaScript
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 2/32
8/29/2020 Beginner JavaScript
• In strict mode, we have to define a variable first before assigning a value to it.
• Scoping:
- var : function scoped (only available inside parent functions)
- let and const : block scoped (available inside a block denoted by { } )
• Opinion (what to use): Use const by default; if the value of the variable needs
to change then use let . Almost never use var .
Types
SNOBUS'N
1. String
2. Number
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 3/32
8/29/2020 Beginner JavaScript
3. Object
4. Boolean
5. Undefined
6. Symbol : Always gives a guaranteed unique identifier
7. Null
Everything except Object is Primitive type, Object is special one.
String
• used for holding text
3. using backticks:
• backticks:
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 4/32
8/29/2020 Beginner JavaScript
• Multi-line string:
1.
2.
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 5/32
8/29/2020 Beginner JavaScript
• Example 1:
const name = 'Soumya';
const hello = 'Hello my name is ' + name + '. Nice to meet you.'
• Example 2:
• Example 3:
Number
Only one type of number in JavaScript whether it has decimal point or not.
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 6/32
8/29/2020 Beginner JavaScript
• Example
The above works with multiplication, division and subtraction and not addition,
because the + sign is also used for concatenation.
• Example
Why? Explanation
So, when working with money, don't store them as dollars and cents. Store all of the
money in cents as you won't have to deal with fractions only whole nos. When need
to display to user, just convert them back.
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 7/32
8/29/2020 Beginner JavaScript
Object
• Everything in JavaScript is an Object.
• Objects are used for collection of data, collection of functionality.
• Example:
• Accessing properties:
When we try to access a variable that is created but not defined/set a value, we
get undefined .
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 8/32
8/29/2020 Beginner JavaScript
• Example:
const age = 18; const ofAge = age > 18; console.log(ofAge); // false
JavaScript
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 9/32
8/29/2020 Beginner JavaScript
• Equal signs:
• == vs === :
10 === "10" // false as values are same but types are not 10 == "10"
// true as values are same
JavaScript
Functions - Built in
• Function allows us to group together multiple statements, take in some
values, perform some operations and return some value.
• Example:
Math.max(10, 12); // 12
JavaScript
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 10/32
8/29/2020 Beginner JavaScript
e.g:
• console.log('hey'); returns undefined , logs hey .
• Many date functions are also present. e.g. Date.now() returns no. of
milliseconds since January 1, 1970 00:00:00 UTC.
• DOM functions:
• Example:
• Other Examples:
Functions - Custom
• Functions are created/ defined then they are called.
• Defining a function:
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 11/32
8/29/2020 Beginner JavaScript
• Calling a function:
• Variables created inside a function are not available outside the function. e.g.
total above.
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 12/32
8/29/2020 Beginner JavaScript
• Parameters are variables local to the function; available only inside the function.
• So, we can either pass direct value or variables holding value or expressions
resulting in a value to a function as arguments.
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 13/32
8/29/2020 Beginner JavaScript
• Default values:
• Important gotcha:
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 14/32
8/29/2020 Beginner JavaScript
Important: These are hoisted, meaning JavaScript 'hoists' or puts them at the top
of the file. So if we try to run a function defined with function keyword before it
is defined/ above its definition, there's no error and the function is executed
successfully.
These are used in callbacks and IIFE: immediately invoked function expressions.
• Function Expression
Important: These are not hoisted, meaning JavaScript doesn't put them at the
top of the file. So if we try to run a function not defined with function keyword
before it is defined/ above its definition, there's an error and the function fails to
execute.
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 15/32
8/29/2020 Beginner JavaScript
• Arrow Functions
• Methods:
• For e.g console.log('hey') : here log is the function and console is the
object.
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 16/32
8/29/2020 Beginner JavaScript
• Callback functions:
• Function that is passed to another function used for something that will
happen when something is done.
Debugging
There are 2 aspects to debugging:
1. Debugging Tools
2. Mindset to tackle errors/bugs etc.
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 17/32
8/29/2020 Beginner JavaScript
1. Console Methods:
• e.g.
// For this data, you get below output in console: const people
= [ { name: "Wes", cool: true, country: "Canada" }, { name:
"Scott", cool: true, country: "Merica" }, { name: "Snickers",
cool: false, country: "Dog Country" } ]; people.forEach((person,
index) => { console.table(person.name); });
JavaScript
useful when we want to check why a function is running more times like
hover element method triggering too many times.
The counting is based on what string we pass to console.count .
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 18/32
8/29/2020 Beginner JavaScript
2. Call Stack or Stack Trace: Tells us what function called what function called
what function and so on..
Example:
In this above example, doesntExist function doesn't exist, which will cause
an error, when we run the go function:
go()
Error:
The error says that this error occurred at line 32 in function greet . greet
was called by go at line 37. The anonymous 1:1 comes as we ran it from our
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 19/32
8/29/2020 Beginner JavaScript
console, else it would have shown the line no. from where we would have
called the go function in code.
3. Grabbing Elements
If we select something using the Elements tab in dev tools, and then flip over
to Console tab, then we run $0 , then we get returned that selected item.
$0 : the selected element
and so on...
step over next function call: runs the code line by line.
We can also set breakpoints from the browser by ourselves in the sources
tab. This does the same thing as a debugger; i.e. stops the JS from running
at that point.
5. Network Requests:
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 20/32
8/29/2020 Beginner JavaScript
We can select an element in Elements tab and right click on it to select break
on > attribute modifications. It means, when somebody changes its
attributes, then there will be a breakpoint.
7. Other types of breakpoints:
In Sources tab, there are other type of breakpoints too like mouse click,
keyboard events, XHR or fetch breakpoints(breakpoint when a XHR request is
made) etc.
Scope
Scope answers the question: "Where are my variables and functions available to
me?".
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 21/32
8/29/2020 Beginner JavaScript
• Global Variables
Available anywhere. They are created not inside a function, module, if statement
etc.
<script> const first = 'Soumya'; </script> //here first is a global
variable.
In the browser, the global scope is called window. Methods like setTimeout,
setInterval are available on the window object.
The above occurs because var variables are attached to the window object and
are globally scoped, whereas const and let variables are globally scoped but
not attached to the window object.
Anything except let and const declared things which are globally declared
are attached to window object.
More examples:
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 22/32
8/29/2020 Beginner JavaScript
to look for the variable in that scope and if its not available in that scope as well,
it goes another level higher.
const age = 100; function go() { const cool = true; const age = 200; //
global variable age has been shadowed by this variable - shadow
variable console.log(age); console.log(cool); } go(); // console
output: // 200 - this is printed because it finds variable age in go
function scope only // true
JavaScript
Block Scope:
if(1 === 1){ var cool = true; } console.log(cool); //Output: true - var
varibales leak outside the block
JavaScript
let cool; if(1 === 1){ cool = true; } console.log(cool); //Output: true
JavaScript
Above the cool variable is declared in the higher scope, here global scope.
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 23/32
8/29/2020 Beginner JavaScript
Above, as the variable cool was declared using var keyword, it was not
limited within the if block, rather it was available inside the entire isCool
function.
let and const variables are block scoped.
Example:
The above is the output because here the function logDog creates a local
variable for parameter dog which has value 'Rufus' passed to it. So, 'Rufus' is
printed.
Function Scoping:
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 24/32
8/29/2020 Beginner JavaScript
Just like variables, functions are scoped to the parent function. So yell function
above is only accessible inside sayHi function, and not outside it.
Hoisting
Allows us to access functions and variables before they are created.
2 things that are hoisted in JavaScript:
• Function declarations
• Variable declarations
Function hoisting
The above happens because when we run the JavaScript file, JavaScript compiler will
take all function declarations and move them to the top of the file available for use.
Suggestion: Always define functions before using them.
Note: Functions made using a variable are not hoisted.
Variable hoisting
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 25/32
8/29/2020 Beginner JavaScript
This happens because JavaScript hoists the variable declaration but not the actual
setting of the values or assignment. This essentially means this happens:
Closures
Ability to access a parent level scope from a child scope even after the parent
function has been closed/terminated.
This happens because even though outer function is done running in the line
const innerFn = outer() , the variable outerVar is still maintained in
memory/accessible at a later time even if it is not returned. It is not cleaned up or
garbage collected.
More examples:
The myGreet variable is still accessible even after createGreeting is done running.
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 26/32
8/29/2020 Beginner JavaScript
score is a private variable here. We can access multiple games at once like that.
• It is everything about the currently open window including the browser bar,
tabs, scrollbars etc.
The document:
• gives info about the browser and the device that it's on
• things like webcam, audio access, battery level, GPS and other device specific
features live on navigator.
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 27/32
8/29/2020 Beginner JavaScript
• we can listen for the DOMContentLoaded event and then try to select elements
from the page.
Best way: Load the JS before the </body> tag.
const p = document.querySelector('p');
JavaScript
• querySelectorAll: Returns all matches as a NodeList (like array but without array
helper functions)
const p = document.querySelectorAll('p');
JavaScript
• .item (element with class of item) or div.item (div with class of item) etc.
• Parent-child selector: e.g. to grab images inside div with class item
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 28/32
8/29/2020 Beginner JavaScript
In above HTML, suppose if we want just the first item and find the image inside of
the item.
We have 2 options:
1. document.querySelector('item img');
2. List
Note: We also have dated/old ways of selecting elements from DOM like
getElementById , getElementsByClassName , getElementsByTagName etc.
In JavaScript:
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 29/32
8/29/2020 Beginner JavaScript
JavaScript:
innerHTML:
outerHTML:
Example:
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 30/32
8/29/2020 Beginner JavaScript
• List
pizzaList.insertAdjacentText('beforeend', 🍕)
JavaScript
Element vs node:
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 31/32
8/29/2020 Beginner JavaScript
https://www.notion.so/Beginner-JavaScript-e2ef045754d14e96b93791f638bbcaf6 32/32