Reviewing the Basics of JavaScript ES6

S M Sabir
4 min readMay 5, 2021
Quick Recap of basic JavaScript

It’s very common to forget syntax, statements and built-in functions when you are a beginner. But no worries it will be okay over time with experience. In this post we’ll recap some most used syntax and functions…

Basic building blocks of JavaScript (others as well)

Here are some basic things you should know.

  1. Variables: Defining variable, Define Arrays, difference between Const & Let,
  2. Statements: If/Else statement, Switch Statement.
  3. Loops: For Loops, While loops, For Each loops
  4. Functions: function parameters, call function, return value, arrow function.
  5. Object oriented Programming concept: Basic understanding of Objects and classes in JS, basic concept of OOP paradigm.

JavaScript Number Types:

Basic arithmetic operation:
Adding two numbers: “+”, ex: const value = 5 + 5; >> value 10
Subtracting : “-” , ex: const value = 5-5; >> value 0
Multiplying: “*”, ex: const value = 5 * 5; >> value 25
Dividing: “/”, ex: const value = 5 /5; >> value 1

Unlikely other Programming Languages like C/C++, we don’t have to define number type like(float, double or int).
But we can do convert to Integer type while doing mathematical operation between two or more numbers.
For example: parseInt(10.5+5); >> it will return 15;

parseInt() built-In function can convert a float to integer, a string (ex: “2.55”) to a number.

Other Use cases of parseInt() is convert strings to base 10 for example.
That should look like :

paresInt(‘020’, 10); >> will return 20
Here the second argument 10 is the base for conversion.

another is to convert binary to integer:
parseInt(‘1010’, 2); >> 10

parseFloat() is another built-in function that we can use to convert as float number.

Ex: parseFloat(5/2); >> 2.5

Strings in JavaScript

Strings is the Unicode characters that we use to write words or sentences but we can keep any number as string inside a double / single quotation. Like: “123” / ‘123’.
So whenever we want to keep something as string, putting that inside a quotation is a must.

Tip: always try to keep inside double quotation show there will no issue if you use single quotation in your sentence.
for example:
Will cause problem => ‘This game is Tommy’s favorite one!’. //
JS will count that the string is ended at y. After that it should be something else.
Good practice => “This game is Tommy’s favorite one!”.

Get the length of a string:
“This game is Tommy’s favorite one!”.length; >> length is 34
or
const string = “This game is Tommy’s favorite one!”;
string.length >> length is 34

String Concatenation (merging strings):

const string1 = “Hello”;
const string2 = “ World”;
const newString = string1 + string2;
Result: Hello World

another approach:

const string1 = “Hello”;
const string2 = “ World”;
const newString = string1.concat(string2);
Result: Hello World

Make Upper Case and Lower Case or Capitalize:

Upper Case:
const string1 = “Hello”;
string1.toUpperCase(); >> HELLO

Lower Case:
const string2 = “WORLD”;
string2.toLowerCase(); >> world

Capitalized:
it’s a bit tricky. If we want to uppercase every first letter of each word then we have to split the words from sting. Let’s see the example.

Only First letter for the sentence:
const string1 = “hello world”;
string1[0].toUpperCase() + string1.substring(1)
Here string1[0] = ‘h’ and string1.substring(1) is the rest of the string.

Let;s know a bit of substring().
substring() is a function that we use to take the rest of the string by specifying a index.
Like: const string1 = “hello world”;
string1.substring(4) will return “o world” because all of the string before index 4 will be excluded.

Before we do the each word Capitalize let’s learn some more function to get it done.

split() — is a function that make pieces of strings by a specified value or character.
for example:
const string = “JavaScript is awesome!”;
const words = string.split(“ ”); // here I have put the space as argument inside quotes.

result: [ ‘JavaScript’, ‘is’, ‘awesome!’ ];

the split function cut the words where there is space in the string and store it to the words array.

Now lets do the Each word capitalize:
const string = “javaScript is awesome!”;
const words = string.split(“ ”);

for (let i = 0; i< words.length; i++){
words[i] = words[i][0].toUpperCase() + words[i].substring(1)
}

if you print this it will return >> [ ‘JavaScript’, ‘Is’, ‘Awesome!’ ]
Why?
In the previous task we separated the words now we have to merge them.

Merging Strings from Array:

Just include words.join(“ ”); after the for loop.

join() — function join string with specified “character / symbol”

join(“ ”) will return the whole array in sentence. if you use join(“ +”) you will see >> JavaScript+Is+Awesome!
As I mentioned earlier, it will join the elements from array with specified symbol or character.

JavaScript Functions:

Regular Function:

function greetings(){

console.log(“Hello There!”);

}

greetings(); // call function to execute(run).

Above function is a very similar function with other programming languages too. We are not passing any value/parameter to the function and the function does not return any.

function greetings(name){

console.log(“Hello ” + name + “!”);

}

greetings(“Tom”);

Now we are passing a value to the function and it include the value in the log.

Arrow Function:

const greetings = () => console.log(“Hello There!”);

greetings();

This function does the same thing as the first regular function. But it’s clean and less code. This feature is introduced in ES6 of JavaScript.
This save a lot of time and keep code cleaner and easy to read when we work in a large project.

const greetings = (name) =>{

const string = “Hello ” + name + “ !”;
console.log(string)
};

greetings(“Tom”);

Take a close look, if we want to make multiple operation or multi-line work inside the function then {} curly braces is required. But for single line it not.

Hope you have leaned something and enjoyed the article. We’ll see you in another one. Thanks!

--

--

S M Sabir

Junior Full Stack JavaScript Developer and Integrated Engineer