Transcript from the "Browser vs Node.js" Lesson
>> So the next thing we want to talk about is actually let's dive right into some of those differences we talked about alert, but browser versus node. So I think the biggest thing here is just like the global space and like what is in the global, right? So if I'm in the browser and I go in here and I type in window, Right, there's a window here.
[00:00:21] Literally everything's attached to the window, right? So, I don't even have to include it. If I say alert, it's implied that the thing to the left of the alert is actually window.alert. I don't know if you knew that, but everything is attached to an object. And [LAUGH] if you don't mention that object, it's assumed that it's a global, it's on the window.
[00:01:06] So there's no such thing as window in Node.js. The alternative to that would be something called literally global, if I can type. So if you type in global, then we get this thing, which is the equivalent of Node.js's window object. We almost never use global, it's just like you would almost never use window.
[00:01:28] It's just there, it's implied, but that's the equivalent there. And you can see some familiar faces in here, right? Clear intervals, set intervals, set timeout. A lot of these things are the same thing that exist in the browser. So, it's different, but not so different. Even fetch now, they have fetch that's seems to be built in here, which is, actually, I didn't know that, that's new, I want to try that out.
[00:01:51] [LAUGH] So, a lot of those things like that. The other thing is now that browsers have modules there's a slight difference on how you interact with modules and create modules in the browser and Node.js. So we're going to talk about modules yet and we will, but I'm bringing this up now just because you've used modules in the browser before, which I don't think a lot of people do directly.
[00:02:16] I think, for the most part, you probably don't. You probably don't do things like this in the browser, but the tools you use do. So if you use something like Vite or Rollup or any other build tool, they'll probably do something like this for you so you don't have to.
[00:03:50] And then the big one here is just the DOM, right? All this cool stuff, you can forget about it. You're never gonna do this a Node because like, I mean, what do you think would happen if I typed this in Node? What would you expect to happen? There's no visual output, there is no webpage, so I don't really know what that would do.
[00:04:08] I can't go reference some element called element ID because there is no HTML, there is no DOM, so couldn't do it. It does get a little confusing because you can do HTML on a server which we will be doing today but even then that HTML doesn't get, executed until it's on the browser.
[00:04:26] So, yeah, there's no DOM stuff here. But the difference is with Node, you can create what's called a server, right. If you don't know what a server is, we'll talk about that as well, but it's basically like a computer that's remote somewhere that responds to requests and can send back files, data, information.
[00:06:21] Obviously not ideal, you would never come in here and build an application because, one, where's it going? It's not saving anywhere. So it's just some place to go to to try some things out. I almost never use it, [LAUGH] but it's here, if you need to, actually you know what, I will come here sometimes for a calculator [LAUGH].
[00:06:41] I'll do math in here, cuz it's just quicker to do math in here sometimes than open up the calculator app, that's like the only time I use this. Otherwise, almost never use it. But it's here for you to explore and try things, maybe copy some code and paste it safely, see what happens.
[00:06:58] But that's the REPL. To get out of it, at least on a Mac, you can hit Ctrl+C, then it'll bring up this message, if you want to exit, press Ctrl+C again or Ctrl+D, or just type exit, like .exit, and it'll go away. But typically, I'll just hit Ctrl+C twice for it to go away, don't use it [LAUGH].
[00:07:21] It's just there because it can be, but you'll never use a REPL like that. Not to be confused with the website called REPL which is really cool, but yeah, you will never use it. And I talk about some of those reasons here, but basically it's not a file, you can't save it, you can't version it, it's just to try some things out.
[00:07:40] Just like you would never build an app in a console, you would never build an app in the REPL.