JAVASCRIPT Interview Questions and Answers

JavaScript often abbreviated as JS, is a lightweight, high level, interpreted or JIT compiled programming language with first-class functions. It is a client-side scripting language.Most well known as scripting language for Web pages. It is based on ECMAScript.


21) How to clone an object in Javascript?

Object.assign() method is used for cloning an object in Javascript.Here is sample usage:

Example : var x = {myProp: "value"};

var y = Object.assign({}, x);


22) How to get an element by class in JavaScript ?

document.getElementsByClassName() method.


23) How do you change the title of the page by JavaScript?

You can change the title of a webpage using setting the title property of the document object.

Example:document.title="My New Title";


24) What is difference between deep and shallow object coping in JavaScript?

Some differences are: Deep copy means copies all values or properties recursively in the new object whereas shallow copy copies only the reference. In a deep copy, changes in the new object don't show in original object whereas, in shallow copy, changes in new objects will reflect in the original object. In a deep copy, original objects do not share the same properties with new object whereas, in shallow copy, they do.


25) How to redirect a page to another page in Javascript?

1) Using location.href: It is the first approach to redirect page. In this,

we can go back to access the original document.

Syntax:

window.location.href =“https://www.onlineinterviewquestions.com”


2) Using location.replace: Another approach to redirect page. In this,

it is not possible to navigate back to the original document by clicking on the back button as it removes the URL of the original document.

Syntax:

window.location.replace(" https://www.onlineinterviewquestions.com");