Most Common Methods Used In Javascript and FAQ



In this post, we are going to see some of the most common Javascript methods which are used, we will see examples of how to use those methods and what are the main arguments that these methods require, we will also discuss in brief some of the common questions asked in Javascript. Please note that this list is compiled by me and does not come from any community vote or poll, so let’s get started.

Javascript every()

This method is used with an array and used to check if all the elements in an array match a certain condition, for example, if all numbers in an array are even numbers or not, if all elements in an array are letters, etc.


let arr = [2, 6, 8, 2, 10];
console.log('result', arr.every(el => el % 2 == 0)); // true

arr[0] = 3;
console.log('result', arr.every(el => el % 2 == 0)); // false

arr = [{"val": 2}, {"val": 4}, {"val": 6}];
console.log('result', arr.every(el => el.val % 2 == 0)); // true

arr[0].val = 3;
console.log('result', arr.every(el => el % 2 == 0)); // false

Javascript every() accepts one required argument which is the callback function and returns either true or false, this callback function gets called with each element in an array and if all the elements match the condition which is inside the callback function then every() returns true otherwise false.

Javascript Object assign()

This method is used to copy properties from one object to another, if the target object has any property with the same key in the source object then the value of that property in the target object gets replaced with the value of the source object.


const obj1 = {"a": 4, "b": 5, "c": 6};
const obj2 = {"c": 7, "d": 8};

const newObj = Object.assign(obj1, obj2); // {"a": 4, "b": 5, "c": 7, "d": 8}
console.log('result', newObj);

Javascript Object assign() takes two arguments, one is the target object and the second argument is the source object, the target is the object where we want to add/modify properties and the source is the object from which properties get added/modified to the target object, we can also dynamically create new objects with existing data.

Javascript slice()

This method is used when we want to extract a part of an array and leave previous and next elements.


const letters = ["a", "b", "c", "d"];
const modifiedLetters = letters.slice(1,4);
console.log('result', modifiedLetters); // ["b", "c", "d"]

Javascript slice() takes two arguments, it takes the starting index in an array from where it will start taking elements into the new array, slice() will keep taking elements up to the second argument position, it will exclude the element at the index value of the second argument.

Javascript Array join()

This method is used to get a single string created from all the elements in an array concatenated and separated by the argument passed to this method.


const arr = ["i", "love", "my", "cat"];
let result = arr.join(' '); // space between blank string
console.log('result', result);

result = arr.join('-');
console.log('result', result);

Javascript Array join() takes a single argument, which is the separator that will be used to separate the string returned.

Javascript random()

This method is used to generate a random floating number between 0 and 1 (less than 1)


console.log('result', Math.random());
console.log('result', (Math.random() * 5 - 1) * 1);

Javascript random() does not take any argument, but we can use it in many different ways to get the random number, for example in the above examples, the first one is going to return a random number between 0 and 1 but less than 1 and the second example will return a random number between two numbers.

Javascript object length and string length

It is very simple to find the javascript object length and string length, let’s see an example of each.


const obj = {"name": "abc", "age": 2};
console.log('length' , Object.keys(obj).length);

const str = "i love my cat";
console.log('length', str.length);

Javascript removeChild()

This method is used to remove the child node from DOM (Document Object Model) and returns the removed node.


<div id ="sp-1">
<span id="sp-2"> This is a span </span>

const parentEl = document.getElementById("sp-1");
const childEl = document.getElementById("sp-2");
const removedChild = parentEl.removeChild(childEl);
console.log('result', removedChild);

Javascript removeChild() takes DOM reference of the child node to be removed, and this method gets called on the parent node of the nested child, as we can see in the above example we have a single div and a span inside it, first, we are referencing the parent div and then calling removeChild() on that reference and passing the reference of child node as the argument.

What is Javascript return?

It is used to stop the execution of a function and return a value of any type, like some of the languages we don’t need to define what type of value is required to be returned from the function, we can return any type of value as required.

What is Javascript continue?

This statement stops the execution of the current iteration of the loop and skips to the next iteration.


let sum = 0;

for (var i = 1; i < 5; i++) {
  if (sum == 3) {
  sum += i;
console.log('result', sum);

How to create a button in Javascript?

It is very easy to create a button in Javascript.

var btn = document.createElement("BUTTON");
var insideTxt = document.createTextNode("This is a button");
btn.onclick = function() {alert('Clicked')}


These are some of the most common methods used in Javascript and some of the FAQs, there will be part 2 of this, with more methods and questions, check out this cool website where you can learn more about Javascript

Check more posts:

AWS Cognito Authentication With Serverless and NodeJS

DynamoDB VS MongoDB: Detailed Comparison

Javascript Math floor(), Math ceil() and Math round()


Please enter your comment!
Please enter your name here