Javascript Insights with Sudhir :
document.getElementbyId('btnResolve');
Is this a Javascript?? --> Mostly No i.e really a lot of this is DOM i.e DOM implemented in javascript
DOM is a interface that supposed to be language neutral, that allows you to manipulate HTML , listen to event handlers and things like that..
so document.getElementbyId is actually a DOM API method which javascript implements
![](https://www.evernote.com/shard/s390/res/1532b510-b042-42ca-b6a2-5054e5d41390.png)
DOM API that allows you to manipulate HTML, listen to event handler ..etc
What happens if below html page is loaded in browser ... i.e after we enter the URL in browser and hit enter
<html>
<head>
<script type='text/javascript>
alert('hello sudhiR');
</script>
</head>
<body>
<h1>HELLO WORLD</h1>
</body>
</html>
Whenever we hit enter , our browser sends an GET request for content to server and the server is going to respond with an HTTP response . In it, its going to include the html i.e string with html tags as above.
--> The browser is going to pass it off to its something like webkit , that will start processing the HTML , it'll parse it and it will start building DOM elements up one-by-one like
<html> --> parsed in to document element
head element to head to DOM and when we get a script tag it goes to javascript interpreter i.e something like v8 which processes javascript and tokenize and we get a popup , untill then all browser processing totally gets freezed until user hits ok and DOM is going take over again
So until the whole DOM is loaded DOMContentLoaded or DOMContentReady will not fire/execute (i.e not fire untill the script executed) i.e untill whole html is processed
DOM creation engines --> webkit , Firefox, trident IE etc...
![](https://www.evernote.com/shard/s390/res/5c36a2d8-d967-4525-a8d4-b1edabbf03be.png)
![](https://www.evernote.com/shard/s390/res/f61b2a8c-431b-4e60-a933-b45a8cecadc6.png)
Doubt: If loaded or access the DOM elemnts in that script tag which is inside head , I think we get undefined.
may be that is why we load at end of body
Bottom Load your scripts..and top load CSS
Javascript Features:
Javascript is a synonym of ECMAScript , ES is a standard and Javascript is like trademark
Javascript is dynamic..
Most other Compiled languages like C .., what happens is we write our C code, we compile it and what compiler does it read that code and convert to in to ByteCode and execute that ... that bytecode is loaded in memory and computer goes instruction by instruction..reading through memory and operating something elese
In dynamic languages like.. there is no distinction b/w the memory that's used for code and the memory that's used for data
![](https://www.evernote.com/shard/s390/res/b6807cdf-1b5e-404b-bddd-d02ce4de0931.png)
Type travels with value and not with variable.. 2
![](https://www.evernote.com/shard/s390/res/32135da6-22fc-4677-96fb-3b6579ca4411.png)
Anything you can do with an object can be done with a function in Javascript..
Create a function, can return a function with inner function, can pass functions as arguments..
The above..is which is dynamically created..
![](https://www.evernote.com/shard/s390/res/9cf52421-7e18-42db-8b3c-7dac1c1860fd.png)
DOM is Javascript representation of HTML and a browser.
there for you to manipulate HTML page, change whats going inside a browser , its an interface provided to javascript
DataTypes OPerators and Primitives:
![](https://www.evernote.com/shard/s390/res/5376c1ed-4fed-447d-98be-0b3777620269.png)
Undefined: Actually going to refer to a pointer , that points hasn't been set. i.e not set this variable or defined yet.
var foo; --> undefined
null --> actually references to null pointer --> i.e its by intent i.e manually set to null. where undefined is not declared which is by mistake
![](https://www.evernote.com/shard/s390/res/a1f3d025-3717-4434-84a7-d17d5b04dc3f.png)
assignment is we are actually taking a pointer and assigning to the address of variable in memory, i.e we have some object in our memory stack and we gonna point foo to that address
No need to use () with new operator,
Delete is deletes a property i.e it doesn't actually delete data , i.e deletes the reference to the object
![](https://www.evernote.com/shard/s390/res/64a59bcc-8f11-47a4-baec-6283f23118fc.png)
Memory Stack implementation in browser:
Memory allocation of browser--> kind of very old one
![](https://www.evernote.com/shard/s390/res/fd7f1e4c-f412-409c-a928-bbf807728620.png)
![](https://www.evernote.com/shard/s390/res/385fcb72-27f1-4556-aab6-2e409f38f8cf.png)
![](https://www.evernote.com/shard/s390/res/97bd1d3a-cd3e-4a86-9fe2-6b8376effd6b.png)
That diagram is before delete.. i.e variables are just pointer to actual memory in call object or context
![](https://www.evernote.com/shard/s390/res/6e2841b9-3d81-4bcb-a726-d79dd0ab61b3.png)
so delete is not actually removing data from memory, it just removes the reference, i.e we are working with pointers either reallocating , reassigning them
So when you have name object it will have some address location and when its assigned to me i.e me.name has address "xxx" location of the object {first:"justin"}, so when me.name is copied to other varibale name then the value stored in me.name is refrence to object i.e address location of {first:"justin"}, so when we delete the property on me i.e me.name it will stop referencing the object {first:"justin"} but the other variable name still has reference or address location of object {first:"jsutin"}
What if delete me--> stille name.first work? --> yes
and important thing about delete is it delete attribute or properties of an object or variable and not variable itself.
delete will return true or false , it returns true if syntax is correct i.e dot something , and false if syntax is wrong
i.e if delete a.name --> true, if you again type same command it will return true bcz it doesnot bother wether that property exists or not
delete a.some --> returns true
delete a --> false
![](https://www.evernote.com/shard/s390/res/195d4e89-97bf-4e01-aa89-57ac0675c079.png)
Why Array is a object , because its stored in memory as below, keys will be integers
![](https://www.evernote.com/shard/s390/res/6bf8d4b1-62cb-4a57-9701-469287840383.png)
so typeOf will return the most base type, except for function which is a special case in javascript
![](https://www.evernote.com/shard/s390/res/99bebddd-8354-4728-96a2-fe944d84f7c5.png)
![](https://www.evernote.com/shard/s390/res/b541ea6c-19e1-40e5-8aca-904a2daef681.png)
![](https://www.evernote.com/shard/s390/res/8054a4df-3512-49e7-81b0-709c1a19001a.png)
![](https://www.evernote.com/shard/s390/res/75ae7e6b-1bc1-42c4-b9f6-47e8a3417919.png)
Gotcha:
when you check in browser typeof name or just type name you will get string i.e "[object Object]"
beacuse name is default property in window object which is string , if you want you can just type name in console , will return ""
![](https://www.evernote.com/shard/s390/res/d1c1b840-b74f-4d09-8616-0b586bca96e9.png)
![](https://www.evernote.com/shard/s390/res/b4a1d7ec-a675-412d-8355-28ea3e64d04a.png)
When Primitive types are compared, with === then they go through the value and type
![](https://www.evernote.com/shard/s390/res/a0ed88ce-8eff-4731-8abc-ff361fe524ae.png)
![](https://www.evernote.com/shard/s390/res/703dece5-df51-413d-8011-aea9cb2aab1f.png)
Now because they are composite types i.e objects its going to check by reference i.e its actually going to check by address of where these objects are in memory . as they are two different objects created in memory they have two different addresses , therefore false
Triple equals in a primitive case compares value at the two addresses and a composite case we 're going to compare the addresses of left and right
![](https://www.evernote.com/shard/s390/res/2b3a2ffd-4603-4a4c-a562-414b6ff0de2b.png)
![](https://www.evernote.com/shard/s390/res/10d16d0e-ce6d-42fa-a20c-202d0064f7e8.png)
![](https://www.evernote.com/shard/s390/res/fc085b6a-6e4c-4cdd-aa0d-de0ed2f49990.png)
![](https://www.evernote.com/shard/s390/res/f43c4c4d-b89a-42a3-ba5c-816e731e9a15.png)
Javascript doesn't have block level scope , so i of this kind is global , its in global call object, so its gonna out the value of i at any given time