A Slight Sketch of JavaScript

Misbah Uddin Faroque
5 min readMay 5, 2021

Though JavaScript is not a new language to explain, but why redeem? Well, I remember somewhere I read that, JavaScript is a very derided as a toy while its powerful features await. So, I think someone should know the basic parts of a language, not only JavaScript. That’s why this redeem operation started. Hope this will be informative.

History

JavaScript was first created in 1995 by Brendan Eich while he was an engineer at Netscape and first released in 1996. At that time, it was named as LiveScript. Then it was renamed as JavaScript.

Source: Via Internet

JavaScript has no concept of input or output. It’s design is based on to run as a scripting language on a host. The host has to deal with the outside world. Our daily used browsers are common host for JavaScript.

Overview

Yes, JavaScript is a multi-paradigm, dynamic language with types and operators, standard built-in objects, and methods. So much likely syntax of Java and C languages. We all know these things. But to redeem, we are making a journey to know the basic again. So, let’s start with JavaScript data type.

Data Type

JavaScript programs manipulate values, and those values all belong to a type. JavaScript’s types are:

  • Number
  • String
  • Boolean
  • Symbol (new)
  • Object (Function, Array, Date, RegExp)
  • null
  • undefined

Numbers

parseInt(): It is a built-in function. One can convert a string to an integer using this function. For example, let’s look at the code below:

parseInt('789', 10); // 789
parseInt('100', 10); // 100
parseInt('090', 10); // 90, as the string is converted, the lead //zero is not counted.

If someone want to convert a binary number to an integer, just change the base

parseInt('1111', 2); // 15

parseFloat(): This function parses an argument and returns a floating point number.

parseFloat(7.89); // 7.89
parseFloat('7.89'); // 7.89
parseFloat(' 7.89 '); // 7.89

isNan(): This method determines whether the passed value is Not a Number or not.

Number.isNaN(NaN); // true
Number.isNaN('hello'); // false
Number.isNaN('1'); // false

String

When one have to use, represent and manipulate a sequence of characters, then string data type is used. Basically, we can call it as an object which is used to hold data that can be represented in normal text form. Its syntax is like:

const myString1 = "This is a string"
const myString2 = 'This is another string'

We can use single and double both quotation to notify a string as well as the caret(``) sign. Now let’s talk about some of the usage of string.

charAt(): To access any character of a string, use charAt() function. For example, to access the first character — a from variable home,

const home = "I have a home";
home.charAt(4); // returns v

length() property: To know the length of a string, use instance property length()

slice(beginIndex[, endIndex]): Extracts a section of a string and returns a new string.

split(): Returns an array of strings populated by splitting the calling string at occurrences of the substring.

trim(): Removes whitespace from the beginning and end of a string. There are also trimStart() and trimEnd() functions.

const computer = '   Hello computer!   ';
console.log(greeting); // " Hello world! ";
console.log(greeting.trim()); // "Hello world!";
console.log(greeting.trimStart()); // "Hello world! ";
console.log(greeting.trimEnd()); // " Hello world!";

replace(searchFor, replaceWith): This method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern could be a normal string or a regular expression.

There are actually tons of methods that can be used by a string. To read a detail of string related information click here

Arrays

JavaScript Arrays can be called a special type of object. Unlike from other programming languages, JavaScript array can hold data of different types. For example, a JavaScript array can consist of number, string, object and mixture of these. As I told before, we will slightly visit JavaScript, only noted some points are discussed below.

To know the length of an array, use .length() property. It will return the exact length of an array.

To access every element/value, JavaScript has now a multiple loops like for(), forEach(), for…of.

To insert an item to the end of an array, use push() method.

To insert an item to the beginning of an array, use unshift() method.

To delete an item from end of an arry, use pop() method.

To delete an item from beginningof an arry, use shift() method.

every(): This method returns true if every element in this array satisfies the testing function.

Now we will discuss some very important function used in array.

filter(): This method creates a new array with all elements that pass the test implemented by the provided function.

const names = ['Samrat', 'Misbah', 'Elina', 'Alvi', 'Khushbo', 'Subah'];const result = words.filter(word => word.startsWith('S'));console.log(result); //  Array ["Samrat", "Subah"]

find(): This method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

const numbers = [95, 6, 8, 130, 4];const result = array1.find(element => element < 7);console.log(result); // 6

splice(): This method changes the contents of an array by removing or replacing existing elements and/or adding new elements in that place.

const names = ['Misbah', 'Uddin', 'Faroque', 'Happy'];months.splice(1, 0, 'Sumaiya'); // inserts at index 1
console.log(months); // Array ["Misbah", "Sumaiya", "Uddin", "Faroque", "Happy"]
months.splice(4, 1, 'Ehsan'); // replaces 1 element at index 4
console.log(months); // Array ["Misbah", "Sumaiya", "Uddin", "Faroque", "Ehsan"]

reduce(): This method executes a reducer function (provided) on each element of the array, resulting in a single output value. Now this is a bit difficult to understand. Look at the example below:

const numbers = [2, 3, 4, 5];
const result = numbers.reduce((sum, item ) => sum + item, 6)
console.log(result); // 20

Here, I have declared the initial value of sum to 6. and item is every element of numbers array.

Math

It is a built-in object that has properties and methods for mathematical constants and functions. This built-in object has many properties. Some of them are fixed value, some are functions. Some examples are given below:

console.log(Math.abs(3-5)); // 2
console.log(Math.ceil(.95)); // 1
console.log(Math.floor(5.95)); // 6
console.log(Math.random()) // randomly returns a float between 0 and 1
console.log(Math.round(0.9)); // 1

JavaScript Functions

There are several types of functions in JavaScript. Normal function, arrow function, anonymous function, callback function etc. There results of work are same, but approach of work and syntax are different. Functions have a great power in any language. But JavaScript made it easier.

JavaScript Variables

New variables in JavaScript are declared using one of three keywords: let, const, and var.

  1. const allows to declare variables whose values will never change in a block.
  2. let and var work as same but have a slightly difference. var is global whether let is block variable(not global)

Comparison in JavaScript

We make comparisons in JavaScript by using <, >, <= and >=. These work for both strings and numbers. Equality is a little less straightforward. The double-equals operator performs type coercion.

789 == '789'; // true
1 == true; // true
789 === '789'; // false
1 === true; // false

Note that the result of comparison using double-equals and triple-equals are different. Why is that? The double-equals operator compares only the value, not type. But the triple-equal operator compares not only the value, but also the data-type too.

--

--