JavaScript Basics — Capitalization, Comments, and Functions
By: John Au-Yeung
Tags: JavaScript, Software Development, Programming, Technology, Web Development

Photo by Nick Fewings on Unsplash

JavaScript is one of the most popular programming languages in the world. To use it effectively, we’ve to know about the basics of it.

In this article, we’ll look at the capitalization and comments on our code, and functions.

Capitalization

Variable names can’t have spaces. However, we probably want to use multiple words.

Therefore, we’ll have to stick them together.

To do that, we can write:

fooBar

foo_bar

foobar

FooBar

Those are the most likely choices.

All lower case is hard to read, so the 3rd way isn't good.

Underscores are a bit painful to type since we’ve to reach for the key.

Therefore, the most common styles are the 1st and the 4th.

The 1st is for most variables while the last is for constructors and classes.

Comments

Raw code may not convey all the information we want to convey to other humans.

That’s why comments exist in code.

It lets us explain things that aren’t explained in our code.

For instance, we can write:

// create tax report from database

let report = new Report();

We may not know that the report data is in a database, so we convey that information.

The // character indicates that it’s a single-line comment.

We can write multiline comments by writing:

/*

* create tax report from database

*/

Now we can write more than one line of stuff.

The /* and */ are characters that denote the start and the end of a multiline comment respectively.

Functions

Functions are the building blocks of JavaScript programs.

We wrap a bunch of code into one group.

For instance, we can define a function by writing:

const cube = num => {

return num ** 3;

}

The cube function takes a parameter num and then return the cube of it.

Functions have a set of parameters, which can be 0 or more and a body, which has a bunch of statements that are run when the function is called.

The body is surrounded by curly braces.

It must always be wrapped in braces except when we have an implicit return of a single statement function.

For instance, we can write:

const cube = num => num ** 3;

since the function is a single statement function and it returns the same value.

Not all functions have to produce a value, but if they don’t they produce undefined .

Scopes

Scopes are a part of a program where the variable is visible.

JavaScript variables can be function, block, or global scope.

Block scoped variables are created with the let and const keywords.

Function scoped variables are created with the var keyword.

And global variables are created with no keyword or as a property of the global object.

Variables created for function parameters or inside a function can only be referenced in the function.

They’re called local bindings.

For instance, we can write:

if (true) {

let y = 20;

const z = 30;

console.log(y + z);

}

Then y and z are both available only inside the if block.

If we have a variable defined before a block and it’s at a higher level, then we can also reference them from within the block.

For instance, we can write:

const z = 30;

if (true) {

let y = 20;

console.log(y + z);

}

and z would still be accessible from the if block.

If we have a var variable:

var x = 1;

Then it’s available to the whole script if it’s at the top level.

We can even reference them before they’re defined, but that wouldn’t be very useful because it’ll undefined before it’s been assigned a value.

Global variables are ones that are available throughout the app.

For instance, if we have:

x = 1;

Then we declared a global variable.

We can’t do that with JavaScript strict mode since we don’t want to accidentally declare global variables.

Photo by Annie Spratt on Unsplash

Conclusion

Capitalization is important for readability. We probably should stick with the naming conventions of JavaScript, which is camelCase or PascalCase.

Functions are important parts of JavaScript programs. Variables’ availability depends on how they’re declared.

Comments can help us explain stuff that isn’t explained in our code.

JavaScript In Plain English

Did you know that we have four publications? Find them all via plainenglish.io — show some love by following our publications and subscribing to our YouTube channel!