Mostly commonly asked snippets in Javascript interviews (Beginners)

Delete an element from the array at any position

const marvels= ["ironMan", "captainAmerica", "blackPanther", "doctorStrange"];
marvels.splice(0,1)
console.log(marvels)
//Output: [ 'captainAmerica', 'blackPanther', 'doctorStrange' ]

Reverse a string:

function reverseStr(string) {
const str= string.split(" ").reverse().join(" ")
console.log(str)
}
reverseStr("Vineet Mishra")//Output: Mishra Vineet

Check if the element has a class

const element = document.querySelector("#element")
element.classList.contains("active")

Get current time

var today = new Date();var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();//Output: 8:44:7

Replace part of a string

const str = "I love medium."
const replacedStr = str.replace("I", "We")

console.log(replacedStr) //Output: We love medium

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vineet Mishra

Vineet Mishra

Full Stack Developer at CCR Tech, I write about Mobile/Web Development and Deployment, and Interview Q & A