Javascript is Event-Driven - What makes Javascript Weird...and Awesome Pt 2
Skills:
JavaScript Fundamentals85%
Key Takeaways
This video explains how JavaScript's event-driven environment makes it unique and powerful, especially in Node.js, and explores the implications of this environment on coding practices and performance.
Full Transcript
okay so let's get into the second thing that makes JavaScript weird and awesome if you just caught this video I'm in the middle of five you'll want to start from video one uh there will be a link in the description uh but the second thing that makes JavaScript weird and awesome this is the one that honestly makes it really really awesome especially if you're doing nodejs um is the event driven environment um and let's kind of get into what that means especially I'll give you an example of how it's different versus pH P um if we have a PHP script here uh and you're load say let's load tweets page two uh the PHP script is going to start doing some things maybe get your username and then go to the database get your tweets put together your HTML page spit out the page and then your script exits and it's done so your PHP script is finished um and you're good to go uh and so at that point p HP is dead it's done it's not doing anything unless you reload the page or load a different page it runs it loads all of its stuff and it dies um and if you rerun it it's going to rerun it again uh JavaScript is completely different JavaScript loads up your page and then it stays in memory waiting it waits for events because JavaScript is not just information it's user events it is Click events it's drag events it's typing it's uh scrolling um and so let's just look at code to give you some more examples this is a really common jQuery code right here it's a document ready function that says um when a button is clicked alert yay I'll show you what this does let me save and refresh click yay click another button yay all right so uh what the what JavaScript is doing when it runs this is nothing actually uh it's running this and remember those first class functions that I showed you the last video here's one right here we're basically giving the document ready function one argument which is this so whenever the document's ready run this function so JavaScript is not actually this script that we have right here is not doing anything until the document is ready then it fires this function which sets up a listener on button so whenever the button is clicked now it's going to fire this function as argument number two so once again JavaScript hasn't done anything again we have a listener for the button click and that's it it's event driven it's listening for a click event on the button um and so the implement the imple implementations the implementations uh what this means is huge uh and so it means that there's this entire ecosystem that has to be built around event driven programming all these other things we're going to into in the next videos are a result of the event driven environment uh it means a couple things it means that this script will fire once and it will not fire again uh just like PHP it doesn't have to refire the script every time something happens it only runs the little piece that has to run because it stays in memory uh let me show you what I mean here let me say ver a equals 1 um in onclick we're going to alert one um and so and actually I'll do that and also go console log setting this up man that's a lot of te's setting this up so we're running some setup script here and then whenever it's clicked we're going to alert not one we're going to alert a so here you go console log setting this up and when I click I don't get another setting this up console log because my whole script didn't run again my whole application did not reun um all this kind of is stayed up in memory the only thing that actually ran is this little function in this one line of the function that's all that ran on my click event so when people say JavaScript is fast that's what they're talking about JavaScript as a language is not actually really really fast but JavaScript has the ability to stay in memory and only fire what it has to Fire and that's what's fast about JavaScript especially node.js um on in nodejs your application runs it's sitting in memory um and when a page request comes through it only fires the code that has to do with that little bitty page request every everything else is already loaded in memory um and so that's kind of what it means to have an event driven environment is we can take this argument function which in this case is a call back to the click event and we can just kind of remember that callback event we can remember that callback function and fire it whenever we need to fire it and that's all we have to do so that's kind of how the event driven environment of JavaScript Works um and that actually creates some problem problems that have to be solved with complex things like closures scope and context let's go ahead and get into those and this is some of the real meat and potatoes um intermediate level JavaScript that we're about to be getting into here
Original Description
This changes EVERYTHING! The fact that Javascript is event-driven is the reason for closures, weird scoping/context, and the myriad of callbacks you see everywhere.
Programmers who are new to Javascript - especially Node.js - are often thrown for a loop by the event-driven nature of JS.
The bottom line is: Javascript isn't built for making a page, it's built for a user-interface. It listens for user interaction and responds accordingly. And when that response happens, it doesn't run any unnecessary code...just the code to respond to the user.
Watch the full playlist:
https://www.youtube.com/playlist?list=PLoYCgNOIyGABI011EYc-avPOsk1YsMUe_
-~-~~-~~~-~~-~-
Also watch: "Responsive Design Tutorial - Tips for making web sites look great on any device"
https://www.youtube.com/watch?v=fgOO9YUFlGI
-~-~~-~~~-~~-~-
Playlist
Uploads from LearnCode.academy · LearnCode.academy · 38 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
▶
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Web Development Tutorial for Beginners (#1) - How to build webpages with HTML, CSS, Javascript
LearnCode.academy
Web Development Tutorial for Beginners (#2) - Basic CSS - How to build a website with HTML & CSS
LearnCode.academy
How to create CSS Layouts - Web Development Tutorial for Beginners (#3) - with HTML & CSS
LearnCode.academy
Bootstrap Tutorial For Beginners - Responsive Design with Bootstrap 3 - Responsive HTML, CSS
LearnCode.academy
Angularjs Tutorial for Beginners - learn Angular.js using UI-Router
LearnCode.academy
CSS Tutorial - Web Development Tutorial for Beginners (#5)
LearnCode.academy
Node.js tutorial for beginners - an introduction to Node.js with Express.js
LearnCode.academy
Github Tutorial For Beginners - Github Basics for Mac or Windows & Source Control Basics
LearnCode.academy
Javascript Tutorial - Programming Tutorial for Beginners Pt 1
LearnCode.academy
Javascript Tutorial - jQuery Tutorial for Beginners Pt 2
LearnCode.academy
AngularJS Directives Tutorial - Part 1 - Demystifying Angular Directives
LearnCode.academy
WATCH THIS IF YOU WANT TO BECOME A WEB DEVELOPER! - Web Development Career advice
LearnCode.academy
YEOMAN TUTORIAL - Master Front-End Workflow with Yeoman, Grunt and Bower
LearnCode.academy
BOWER! - Streamline Web Workflow with Bower Package Manager
LearnCode.academy
Chrome DevTools for CSS - Better CSS Coding & CSS Debugging with Developer Tools
LearnCode.academy
GITHUB ATOM - Why Atom.io will be your favorite Text Editor!
LearnCode.academy
GITHUB PULL REQUEST, Branching, Merging & Team Workflow
LearnCode.academy
Pimp that Terminal - Add shortcuts and functions to your .bash_profile to simplify routine tasks
LearnCode.academy
jQuery Tutorial #3 - Writing Smarter, Better Code - jQuery Tutorial for Beginners
LearnCode.academy
jQuery Tutorial #2 - Event Binding - jQuery Tutorial for Beginners
LearnCode.academy
jQuery Tutorial #1 - jQuery Tutorial for Beginners
LearnCode.academy
Node.js MongoDB Tutorial using Mongoose
LearnCode.academy
Node.js tutorial for beginners 2014 - an introduction to Node.js with Express.js
LearnCode.academy
WEB DEVELOPMENT - SECRETS TO STARTING A CAREER in the Web Development Industry
LearnCode.academy
jQuery Tutorial #4 - DOM Traversal with jQuery
LearnCode.academy
jQuery Tutorial #5 - Building a jQuery Tab Panel Widget
LearnCode.academy
jQuery Tutorial #6 - Building a jQuery Image Slider
LearnCode.academy
jQuery Ajax Tutorial #1 - Using AJAX & API's (jQuery Tutorial #7)
LearnCode.academy
jQuery Ajax Tutorial #2 - Posting data to backend (jQuery tutorial #8)
LearnCode.academy
jQuery Ajax Tutorial #3 - Delegating Events & Mustache.js Templating (jQuery tutorial #9)
LearnCode.academy
jQuery Ajax Tutorial #4 - "Edit" modes & Better Mustache.js Templating (jQuery tutorial #9)
LearnCode.academy
How to put your website online - how to FTP to a domain & upload files to a webhost
LearnCode.academy
Basic Terminal Usage - Cheat Sheet to make the command line EASY
LearnCode.academy
SSH Tutorial - Basic server administration with SSH
LearnCode.academy
Vagrant Tutorial - Running a VM For Your Local Development Environment
LearnCode.academy
Sublime Text Favorite Packages and Workflow
LearnCode.academy
What Makes Javascript Weird...and AWESOME - Pt 1
LearnCode.academy
Javascript is Event-Driven - What makes Javascript Weird...and Awesome Pt 2
LearnCode.academy
Javascript Closures Tutorial - What makes Javascript Weird...and Awesome Pt 3
LearnCode.academy
FREE REST API - Practice Developing Javascript AJAX Apps with this API
LearnCode.academy
Javascript Scope Tutorial - What Makes Javascript Weird...and Awesome Pt 4
LearnCode.academy
Javascript Context Tutorial - What makes Javascript Weird...and Awesome Pt5
LearnCode.academy
Nginx Tutorial - Proxy to Express Application, Load Balancer, Static Cache Files
LearnCode.academy
Live Reload Sublime, Chrome, Anything - Fast and easy with Live-Server
LearnCode.academy
Are you bad, good, better or best with Async JS? JS Tutorial: Callbacks, Promises, Generators
LearnCode.academy
Javascript Generators - THEY CHANGE EVERYTHING - ES6 Generators Harmony Generators
LearnCode.academy
Web Development Advice - Interview with Dev Tips
LearnCode.academy
How the Internet Works for Developers - Pt 2 - Servers & Scaling
LearnCode.academy
How the Internet Works for Developers - Pt 1 - Overview & Frontend
LearnCode.academy
HAPROXY vs NGINX - 10,000 requests while killing servers
LearnCode.academy
Node.js Cluster - Boost Node App Performance & Stability with Clustering
LearnCode.academy
Web Dev Training with Treehouse
LearnCode.academy
What is Node.js Exactly? - a beginners introduction to Nodejs
LearnCode.academy
How to deploy node.js applications #1 - spin up a server
LearnCode.academy
Deploying node.js applications #2 - provision server & setup flightplan
LearnCode.academy
Deploying Node.js Applications - Deploy Node the right way - as an Upstart Service
LearnCode.academy
Mobile Web Design - Coding Workflow For Mobile Websites
LearnCode.academy
WHY YOU NEED A BUILD SYSTEM LIKE GRUNT, GULP, BRUNCH FOR YOUR WEBSITE
LearnCode.academy
GRUNT TUTORIAL - Grunt makes your web development better!
LearnCode.academy
STOP USING FTP! - How to Deploy with Flightplan over SSH
LearnCode.academy
More on: JavaScript Fundamentals
View skill →Related Reads
📰
📰
📰
📰
A tabbed form that silently refused to submit — required fields hidden behind another tab
Dev.to · Susumu Takahashi
The DOM Isn’t Slow. The Way Most Code Talks to It Is.
Medium · Programming
The DOM Isn’t Slow. The Way Most Code Talks to It Is.
Medium · JavaScript
Vanilla JavaScript — Why It Still Matters in the Age of React and Next.js
Dev.to · Osama Abu Motlaq
🎓
Tutor Explanation
DeepCamp AI