Coding for Kids

JavaScript Keywords and the "use strict" Directive

Keywords

Keywords are reserved words that you can't use for anything other what they were created for. These are used for things like declaring variables, starting loops, creating functions, and more. Each one of these serves a purpose and you can't change that.

Coding for Kids Sidenote: What if you really want to make a variable or function that has one of these names? Your only real option is to change the capitalization, like New or CATCH. However, this isn't a good idea. It clutters up your code and makes it confusing. When you see the word function in your code, it should mean you are creating a function, not having a variable that's called FUNCTION. Best practice? Avoid using these words as names, even if you change the capitalization. Come up with another way to name your stuff.

For example, you can't create a variable called new or false or function. Each one of these has a special job to do.

Here is a list of keywords in JavaScript. Notice that almost all of them are lowercase. A few of these are technically called reserved variables, not keywords, but the concept is the same. They all ready have a purpose and you can't redefine them.

abstract delete if null throws
await do implements package transient
boolean double import private true
break else in protected try
byte enum Infinity public typeof
case export instanceof return undefined
catch extends int short var
char false interface static void
class finally let super volatile
const float long switch while
continue for NaN synchronized with
debugger function native this yield
default goto new throw

See the official list of keywords here.

The "use strict"; Directive

This directive tells JavaScript to be more picky (a.k.a. strict) when it deciphers your code. For example, it is possible to declare a variable without using the let keyword. This makes it a global variable that any function can use, which can be both good and bad. You can easily, and purposely, set your own global variable just by declaring them outside of any function.

This directive controls for other things, too. It will throw error in places where JavaScript would otherwise convert something or ignore a problem.

So if JavaScript sort of fixes these things for you, why would you even want to use this mode? Especially while you're learning it's more important to learn the proper way to write code. It is important to learn the right way to declare variables and so on.

To activate this mode, all you need to do is put this string at the very top of your code, typed exactly as is.

I would recommend that while you're learning, you always make this the top line of your code. It will help you to be a better programmer.

If you would like to see the full list of pros and cons (yes, there are some), you can check out the official documentation.

Strictly speaking, you've got all the keys you need to succeed!

—Dr. Wolf