Introduction

You’ll learn about the topics this chapter contains, which include variable declaration using let and const.

We'll cover the following

Before we begin, I have a question for you. How many variables did you declare in your code yesterday? It doesn’t matter what language you were writing. Was it ten? A hundred? How about over the last week? Last month? Probably a lot.

Now think about how many variables you read yesterday. Maybe you read your own code, or maybe you were skimming someone else’s. Did you see a hundred variables? a thousand? Chances are, you don’t have a clue.

Now if I asked you how many curried functions you saw yesterday, I bet you’d know the answer. I can tell you that I saw exactly one curried function yesterday. I know that because even though there’s been lots of ink spilled about curried functions in JavaScript (and I’ll be spilling some myself in Tip 34, Maintain Single Responsibility Parameters with Partially Applied Functions), it’s not nearly as common as a simple variable declaration. In fact, if you’ve never heard of a curried function, that’s even more proof that they aren’t nearly as important as a simple variable declaration. We spend so much time thinking and teaching complex concepts, but something as simple as variable declaration will affect your life and the lives of other developers in a much more significant way.

What does this chapter include?

You’re about to rethink JavaScript code from the ground up. And that means you need to start at the most basic level: assigning information to variables. So that’s the theme for our first chapter.

Modern JavaScript has several new ways to declare variables. Whenever you start to write a variable, you just need to ask yourself if this will make the code more readable and predictable for the next developer. You’ll find that it actually changes how you write quite a bit.

You’re going to look at two new variable declaration types. The first, const, doesn’t allow you to reassign the variable (which you’ll see is a good thing).

The second, let, will allow reassignment, but it’s block-scoped and will protect you from potential scope conflicts. Finally, you’ll learn how to use template literals to create new strings from your variables.

The tips in this chapter will help you understand how your decisions will affect the rest of the code, and also how your decisions will affect anyone else who might eventually pick up and read your code.

I hope that as you read this chapter, you begin to critically examine the JavaScript that you write every day. The bonus is that with just a handful of tips, you’ll be well on your way to writing JavaScript code that’s more simple and expressive. And don’t be surprised if the mindset you learn when assessing variable declarations flows out into the rest of your code. After all, it’s the most common decision you’ll make while you write—a decision you’ll make 10, 20, 100 times tomorrow, and next week, and next month.


Ready? Good. Let’s begin.

Get hands-on with 1200+ tech skills courses.