Course info
Apr 5, 2016
2h 39m

If you've ever experienced problems in JavaScript but don't have the knowledge of how to fix them, this course is for you. In JavaScript Best Practices, you will learn best practices in several areas areas of JavaScript. First, you will look at syntax oddities in JavaScript, so you can understand the best way to write your JavaScript code, discovering where these issues arise and how to prevent them using strict mode. Next, you'll learn how to deal with callbacks (the most confusing and error-prone part of JavaScript) and how to deal with them in a low-maintenance way. Finally, you will close out the course with a look at Node.js production practices and issues that could crop up due to cross-platform differences. By the end of this course, you will have the skills needed to prevent common issues in JavaScript, saving yourself time and energy while coding.

About the author
About the author

Jonathan is a Pluralsight Author, Technology Advisor, and Business Leader. As a member of the Chief Digital Advisory team at World Wide Technology, Jonathan is able to leverage his unique experiences and skills to drive digital transformation for his clients. As a dedicated developer community leader, Jonathan serves on the board of directors for the Kansas City Developers Conference, is a Microsoft MVP, and is a regular speaker and keynote presenter at conferences around the globe.

More from the author
RESTful Web Services with Node.js and Express
2h 2m
Jan 24, 2019
More courses by Jonathan Mills
Section Introduction Transcripts
Section Introduction Transcripts

Course Overview
Hey everyone. My name's Jonathan Mills and welcome to my course to JavaScript Best Practices. As a consultant, I get to work with companies who are trying to adopt to the current stack of JavaScript technology such as angular or no-js. And in this course, we're going to talk about some of the struggles that developers run into when they start writing JavaScript. So in this course, what we want to do is look at some of the best practices around JavaScript. And instead of just telling you, "here's what you need to do. " We're going to take the time and I'll walk you through why you need to do the things that we're talking about. So we're going to approach this conversation from a couple of different directions. The first one being, we're going to talk about syntax best practices. And we're going to talk about things like semi colons and IIFEs and curly braces and those types of things to make sure that the code we write is being written in a way that makes sense. The next thing we're going to talk about is more behavioral, the way JavaScript behaves. And we're going to talk about the vis keyword or strict mode and what strict mode actually does. Then we're going to move into more structural components like callbacks and promises and the module pattern and things like that. So that we understand, not just the syntax that we write, but the bigger concepts of what we're trying to write when we write code. And we're going to end this conversation looking at mostly the server-side and how to set up no-js applications to be successful in a production environment. So by the end of this course, hopefully you would have picked up some tips on how to not only how to write better JavaScript but also have a clear understanding of why JavaScript is behaving the way it does in your code.

Why Best Practices?
Now a conversation about best practices can sometimes be a little painful for everybody involved because the term best is nebulous at best and just wrong at worst and so what I want to do is I want to spend some time in this first module talking about why we need or why we should discuss some best practices and kind of the approach we're going to take when we do it. Now, I've noticed over the years that JavaScript is traditionally a secondary language for a lot of developers. They operate most of their lives in C-sharp or Java or Ruby or something else and then they use JavaScript to make their browsers work and a lot of their time and effort is spent on being very, very good at a language other than JavaScript with very little time being spent on the JavaScript side of your job. Now that's changing with the rise of single-page applications that have come about to make this very rich browser applications that more and more of our customers are starting to expect and we're starting to spend more and more of our time on the JavaScript side to where JavaScript is almost becoming the primary language and the other languages are almost secondary for just serving up our APIs or something like that and with the rise of node, JavaScript's taking over the server and we've got now mobile frameworks like React Native that are taking over our mobile development as well. So JavaScript is starting to be everywhere in what we do. On top of that, JavaScript's just different, it just is. It's different than the other languages that we work on and sometimes that's really good and sometimes that's really, really bad and so what I want to do is I want to spend some time talking about why we need to look at some of the things that are different about JavaScript and then go into what the differences are and the best way to understand what's going on inside JavaScript

So, in this module we're going to talk about some JavaScript behaviors. And this is going to tie into the conversation we were just having about syntax. But I'm going to expand that conversation a little bit and talk about some of the things that JavaScript does that you may not know that it does. And it may be a little weird. So, we're going to talk about, like I said, things that JavaScript does that you don't necessarily expect. And when we're talking about semicolons, and semicolon insertion, this RSpec actually says, as a matter of convenience, JavaScript's going to stick semicolons in there for you. It's going to do it because it's trying to be helpful. It's trying to be nice. Well, there's a lot of things JavaScript does to be helpful. Either to protect you from errors or to, you know, just make things, maybe, better. And that's not always a good thing. Because sometimes JavaScript is doing something that you don't want it to do. Or you don't know that it's doing and you think you're actually doing something you're not. And so, in this module I'm going to walk you through some of those things, I'm going to walk you through some of these weird behaviors that JavaScript has that you may not even know are happening. Probably not, there's a lot of these that a lot of people think they're doing that they're not actually doiing, because JavaScript is protecting them. So, in this module we're going to kind of dig into what those look like, and we'll talk about strict mode. And how to prevent JavaScript from doing some of these things that you don't want it to do.

Async Patterns
Alright, so in this module we're going to talk about Async patterns or some ways to deal with asynchronous code that's executed in JavaScript. And this is usually kind of a pain point for a lot of people who are entering JavaScript development because it's just so different than everything else that's out there. And so what we're going to talk about is these Async patterns and how to deal with asynchronous code. We're going to start by talking about callbacks and callbacks are usually where people become frustrated with JavaScript and they start to throw in the towel and say, "This is stupid", because you get what they call "callback hell" or "Christmas tree code" or just really gross looking code as you're dealing with callback after callback after callback. We'll look at some good ways to write callback code but then we'll also add in some promises and we'll talk about the way promises work and how they help separate out your parameters from your function code. And last but not least we're going to look at async, we're going to look at the Async/Await that's coming in up at ES7 and how you can start to implement the Async/Await pattern into your code today using a transpiler called "Babel".

Production Code
In this module we're going to talk a little bit about production code and having code exist in our production environment and we're going to focus on Node. js in this case because that's where a lot of these gotchas are going to get you. So we'll talk about some Node. js best practices and we'll talk about things like npm and so the best way to use npm and how npm works a little bit. We'll talk about setting up our environment. Instead of just hard-coding everything, we'll have some environmental variables and we'll pull out of the environment. We'll talk about some cross platform gotchas. Node's awesome for cross platform, but Windows is not Mac is not Linux and so we'll talk about a couple of the gotchas that come from being in a cross platform environment and we'll end with a conversation about simplicity. Really what I want everybody to walk away from here is to simplify their JavaScript a little bit. Things have become kind of out of control and spiraled into this big monolithic thing and I think this year especially we're going to start to simplify our JavaScript quite a bit and we'll talk about that also towards the end.