10 Concepts of Javascript To Improve Your Programming Skill

Md. Faizur Rahman Khan
5 min readNov 2, 2020
Concepts of Javascript
Lets Learn Some Core Concepts of Javascript

Numbers in Javascript

1. Number.isNaN():

In Javascript, the Number is NaN() is a method that determines that the passed value is NaN or not. The syntax is Number is NaN(value). It means the value should be in number form but it is not a number. That means NaN.

Summary:

  • In Number isNaN() method, there is no problem of forcefully converting the parameter to a number.
  • This is the safest method to pass values that normally converted to NaN.
  • Only number type values that are also NaN, those values return true.
Number.isNaN(123) //false
Number.isNaN('123') //false
Number.isNaN('Hello') //false
Number.isNaN('') //false
Number.isNaN(true) //false
Number.isNaN('NaN') //false
Number.isNaN(NaN) //true
Number.isNaN(0 / 0) //true

String in Javascript

Strings are used to hold data that is in text form. The operations on strings are to check their length, to build and concatenate them using the string operators, checking for the existence or location of substrings with the indexOf() method, and extracts substrings with the substring() method.

2. String.concat():

This method concatenates the string arguments. It is applied to the calling string and it returns a new string. The syntax is str.concat ().

Summary:

  • The concat() function concatenates the string arguments to the calling string.
  • It returns a new string.
  • It changes to the original string or the returned string and doesn’t affect the other.

Join Three Strings Example:

var str1 = "Hello";
var str2 = "world!";
var str3 = " Have a nice day!";
var res = str1.concat(str2, str3); // Hello world! Have a nice day!

3. String.includes()

The includes() method determines that one string is found within another string or not. It returns true or false as result. The syntax is str.include(). It means one string is included in another string or not.

Summary:

  • This method determines whether a string includes another string or not.
  • The includes() method is case sensitive.

For example, the following expression returns false:

'Hello World'.includes('world') // false

Examples:

var str = "Hello world, welcome to the universe.";
var n = str.includes("world");//true

4. String.replace()

The replace() method returns a new string with some or every matches of a particular pattern. The pattern can be a string or a regExp. The replacement can be a string or a function. It is called for each match of the pattern. If the pattern is a string, only the first occurrence will be replaced. The original string remains unchanged. The syntax is const newStr = str.replace(). It is applied to replace any kind of string in a pattern.

Summary:

  • This method does not change the calling string object.
  • It simply returns a new string.
  • To perform a global search and replace, it includes the g switch in the regular expression.

Examples:

var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "Google");//Visit Google!

5. String.split()

The split() method divides a string into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is for a pattern. The pattern is provided in the methods call as the first parameter. The syntax is str.split(). It is applied to split any string at any specific point.

Summary:

  • When it found true, the separator is removed from the string, and the substrings are returned in an array.
  • The separator is a regular expression with capturing parentheses, then each time the separator matches. Then the results of the capturing parentheses are spliced into the output array.
  • If the separator is an array, then that Array is coerced to a String and used as a separator.

Examples:

var str = "How are you doing today?";
var res = str.split(" ");//How,are,you,doing,today?

Array in Javascript

6. Array.concat()

The concat() method is used to merge two or more arrays. This method does not change the existing arrays. Instead, it returns a new array. The syntax is const new_array = old_array.concat(). It is applied to concat any array like object at any particular point.

Summary:

  • The concat() method creates a new array consisting of the elements in the object.
  • It does not recurse into nested array arguments.
  • The method does not alter this or any of the arrays that is provided as arguments.
  • It returns a shallow copy.

Examples:

var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale);//Cecilie,Lone,Emil,Tobias,Linus

7. Array.filter()

The filter() method creates a new array with all the elements that pass the test that is implemented by the provided function. The syntax looks like let newArray = arr.filter(callback()) ([]). It is used to filter any object from an array.

Summary:

  • filter() calls a provided callback function once for each element in an array and constructs a new array of all the values.
  • A callback is invoked only for indexes of the array.
  • It is not invoked for indexes that have been deleted or which have never been assigned values.

Examples:

var ages = [32, 33, 16, 40];
function checkAdult(age) {
return age >= 18;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.filter(checkAdult);
} // 32,33,40

8. Array.reduce()

The reduce() method executes a reducer function on each element of the array, and results in a single output value. It is applied to an array to get a single output value by executing a reduce function.

The reducer function takes four arguments:

  • Accumulator (acc)
  • Current Value (cur)
  • Current Index (idx)
  • Source Array (src)

The syntax looks like arr.reduce(callback( ) { }[]); The return value is the single value that results from the reduction.

Summary:

  • The reduce() method executes the callback once for each assigned value present in the array, and takes four arguments.
  • The first time the callback is called, accumulator and currentValue can be one of two values.
  • If initialValue is provided in the call to reduce(), then the accumulator will be equal to initialValue, and currentValue will be equal to the first value.

Examples:

var numbers = [175, 50, 25];
document.getElementById("demo").innerHTML = numbers.reduce(myFunc);
function myFunc(total, num) {
return total - num;
} //100

9. Array.reverse()

The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first. The syntax is a reverse(). It is used to reverse an array by applying the reverse() method.

Summary:

  • The reverse method transposes the elements of the calling array object in place, mutating the array, and returning a reference.
  • The reverse is intentionally generic.
  • This method can be called or applied to objects resembling arrays.

Examples:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.reverse();// Mango,Apple,Orange,Banana

10. Array.unshift()

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array. The syntax is arr.unshift([, …[]]). It is applied to unshift any array by adding one or more elements at the start of an array. It gives a new length to that array.

Summary:

  • The unshift() method inserts the given values to the beginning of an array.
  • unshift() is intentionally generic.
  • Objects that do not contain a length property reflects the last in a series of consecutive, zero-based numerical properties.

Examples:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon","Pineapple"); // Lemon,Pineapple,Banana,Orange,Apple,Mango

--

--