Categories
JavaScript

Good Parts of JavaScript — Strings and Flow Control

Spread the love

JavaScript is one of the most popular programming languages in the world. It can do a lot and have some features that are ahead of many other languages.

In this article, we’ll look at some good parts of JavaScript, including strings and flow control.

Strings

Strings can be wrapped in single or double quotes or backticks.

It can contain 0 or more characters. The “ (backslash) is an escape character.

JavaScript doesn’t have a character type. We just put one character in a string to represent one character.

Single or double quotes are good for delimiting short strings that don’t have any dynamic parts inside.

Backticks are used for delimiting template strings, which can contain expressions and can be multiple lines long.

Therefore, whenever we can, we should use template strings.

It’s much more convenient than using regular strings.

We can also use the single or double quotes within template strings as normal characters rather than use it to delimit strings.

For instance, we can define strings in the following ways. We can define a string with single quotes:

const foo = 'foo';

To define strings with double quotes, we can write:

const foo = "foo";

or we can define template strings by writing:

const world = 'world';
const greeting = `hello ${world}`;

Template strings can contain expressions, which can optionally contain expressions.

Expressions are surrounded by the ${} characters.

This is great since dynamic string replaces concatenation, which is a pain to use if we have to create dynamic strings.

We can create multiline strings without hassle as follows:

const hello = `
hello
world
`

Now 'hello' and 'world' will be in separate lines.

We can also use template tags to return a value by passing in a template string.

Template tags are just functions with special signatures.

The first parameter of a template tag is an array of strings, and the remaining parameters are the expressions.

If we write:

const tag = (strings, ...exps)=>{
 console.log(strings, exps);
}

const name = 'world';
tag`hello ${name}`;

We get that strings is [“hello “, “”, raw: Array(2)] and exps is an array of expressions, which is [“world”] .

All strings have a length property. It returns the length of the string.

JavaScript strings are immutable. Once it’s made, it can never be changed.

But we can create new strings by concatenation or putting them in template strings.

2 strings that have the same length and exactly the same characters in the same order are considered the same.

So:

'foo' + 'bar' === 'foobar';

returns true .

JavaScript strings also have many methods that we can use to manipulate them.

Photo by Samuel Ferrara on Unsplash

Statements

JavaScript programs are composed of one or more statements.

If we use script tags only, then everything is thrown into the global namespace.

However, with the introduction of JavaScript modules, modules can be bundled together to compile them into a big bundle.

let or var can be used to declare variables. And they’re private if they’re declared in a function.

let is also private within the block.

const defines a constant, which is also block scoped.

Constants are mutable, but we can’t assign them to a new value.

The switch , while , for and do statements are allowed to have an optional label statement that can be used with the break statement.

Statements are usually run from top to bottom.

The sequence can be changed with conditional statements, if and switch .

Loop statements, while , for , and do can also change the order of execution of statements.

Disruptive statements like break , return , and throw can also change the execution flow.

Function invocation can also change the flow of the program.

A block is wrapped in curly braces. Blocks create a new scope for any data declared with let or const .

Therefore, it’s preferred that we use let or const to declare our variables and constants.

An if statement changes the flow of a program based on the value of the expression within the parentheses.

If it returns true , then the code in the if block is run.

Otherwise, else if ‘s conditions will be check and the code in those blocks will run if possible.

If an else block is present, that will run if other blocks before it isn’t run.

Conclusion

Template strings are great. We can have expressions and they can be more than one line long.

Statements are building blocks of programs. We can run the flow can change based on various flow control statements.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *