Javascript interview question set 1/JavaScript Interview Questions and Answers for Freshers & Experienced

How can you convert the string of any base to integer in JavaScript?

The parseInt() function is used to convert numbers between different bases. It takes the string to be converted as its first parameter, and the second parameter is the base of the given string.

For example-

parseInt("4F", 16)

Explain the terms array slice() and array splice()?

1. Array slice() method removes the array items from the array and reforms the removed items into a new array. The selected items through an array slice() method are removed from the array and formed as another new array.

2. array splice() method removes the items of the selected array from the array and does not form another array.

What is the meaning of the word 'callback'?

The callback is a typical function of JavaScript that can be passed as an option or argument of JavaScript. Sometimes, callbacks can also be termed as simple events. Users are given calls to react to different kinds of triggered situations.

What is the difference between ‘function declaration’ and ‘function expression’?

!DOCTYPE html>
<html>
<body>
<h2> <strong> Sample: Software Testing Help</strong> </h2>
<p style='text-decoration:underline'>Example Function Declaration</p>
<p id="display_add"></p>
<p id="display_sub"></p>
<script>
function add(first_num,second_num){
return first_num + second_num;
}
var substract = function sub(first_num,second_num){
return first_num - second_num;
}
var first_num=700;
var second_num=300;
document.getElementById("display_add").innerHTML = "Sum of the number is:" + add(first_num,second_num);
document.getElementById("display_sub").innerHTML = "Difference of the number is:" + substract(first_num,second_num);
</script>
</body>
</html>

As shown in the example add() is a function declaration and subtract() is a function expression. The syntax of the function declaration is like a function which is saved into a variable.

Function declarations are hoisted but function expressions are not hoisted.

How do JavaScript primitive/object types passed in functions?

One of the differences between the two is that Primitive Data Types are passed By Value and Objects are passed By Reference.

What is NaN in JavaScript?

NaN is a short form of Not a Number. Since NaN always compares unequal to any number, including NaN, it is usually used to indicate an error condition for a function that should return a valid number. When a string or something else is being converted into a number and that cannot be done, then we get to see NaN.

In case you are facing any challenges with these JavaScript Interview Questions, please comment on your problems in the section below.

What are the different ways to access HTML element in JavaScript?

The following DOM Methods are used to capture the HTML element and manipulate it.

01. getElementById('idname') - > This function is used to select the HTML element based on ID

example:-

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<label id="myelement"></label>
<script>
document.getElementById('myelement').innerHTML = '<h3> Welcome </h3>'
</script>
</body
</html>
02. getElementsByClassName('className') - > This function is used to select the HTML elements based on the class name in DOM, it will return all matched HTML element with respect to the class name.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<style>
.lblMsg {
color: #000;
}
</style>
<body>
<label id="myelement" class="lblMsg"></label>
<script>
document.getElementsByClassName('lblMsg')[0].innerHTML = '<h3> Welcome </h3>'
</script>
</body>
</html>
03. getElementsByTagName(‘HTMLtagname’)
> This function is used to select the HTML elements based on the Tag name in DOM, it will return all matched HTML element with respect to the Tag name.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<style>
.lblMsg {
color: #000;
}
</style>
<body>
<label id="myelement" class="lblMsg"></label>
<script>
document.getElementsByTagName('label')[0].innerHTML = '<h3> Welcome </h3>'
</script>
</body>
</html>

What is BOM?

BOM (Browser Object Model) which provides interaction with the browser, the default object of the browser is a window. Various property provided by windows is a document, history, screen, location, navigator.

What is the difference between the operators ‘==‘ & ‘===‘?

The main difference between “==” and “===” operator is that formerly compares variable by making type correction e.g. if you compare a number with a string with numeric literal, == allows that, but === doesn’t allow that, because it not only checks the value but also type of two variable, if two variables are not of the same type “===” return false, while “==” return true.

Does JavaScript support Automatic type conversion?

Automatic type conversion is supported by javascript and usually, the developers of javascript use the automatic type conversion method.

How to redirect the user to a new page?

We can use the window object location to redirect the user to the new page

window.location.href="https://www.dotnettricks.com/"

What are the different ways a JavaScript code can be involved within an HTML file?

The three different ways:

Inline
External
Interna

The JavaScript function known as the inline function is assigned to a variable that is created at runtime. On the other hand, if you require a JavaScript for function, you can integrate the script on the page on which you are working or you can place it as a separate file which can be called, when needed. This essentially, becomes the difference between external and internal script.

How can you use JavaScript to read a cookie?

You can read a cookie as simply as creating a cookie in JavaScript as it is actually the value of the document.cookie object. If you want to access that specific cookie, you can use this string any time.

1. By using the document.cookie string, you can keep a list of name – value pairs which are separated by semicolons, where the name is actually the name of a cookie and the value is the string value.

2. You can also make use of strings’ split() function to break the string into values and keys.

How can you create a cookie with the help of JavaScript?

You can create a cookie in JavaScript simply by assigning a string value to the document.cookie object.

The syntax:

document.cookie = “key1 = value1; key2 = value2; expires = date”;

What is the work of the TypeOf Operator?

The typeof operator can be used to get the datatype of its operand. The specified operand can be a data structure or a literal such as a function, object or a variable.

What are a few conventions of naming variables in JavaScript?

A few rules are:

1. One should not use any JavaScript reserved keyword as the variable name.
2. Variable names in JavaScript cannot start with a numerical that is within 0-9.
3. Variable names in JavaScript are case sensitive.

Define Closure?

Closure is developed when a specific variable is defined outside the current scope and it is accessed from within with some inner scope.

How to submit a form in JavaScript?

In JavaScript, we can submit a form by using

document.form[0].submit();

What is mean by “this” in javaScript?

“This” keyword is an object in a javascript, where it refers to. It has different values at different stages, whereas in method “this” is used as an owner object and in function, it is called a global object.

Mention the different types of functions that are supported by JavaScript? Define each of them?

There are two types of functions that are supported by JavaScript. They are -

Anonymous function- This type of function generally has no name and this is the difference between it and a normal function.

Named function- On the other hand, this function is named properly and specifically.

What are the ways used to read and write a file in JavaScript?

Two ways are available to read and write a file, they are:

1. JavaScript Expressions
2. Webpages and Active X objects

What are the ways to define a variable in JavaScript?

The three possible ways of defining a variable in JavaScript are:

Var – The JavaScript variables statement is used to declare a variable and, optionally, we can initialize the value of that variable. Example: var a =10; Variable declarations are processed before the execution of the code.
Const – The idea of const functions is not allow them to modify the object on which they are called. When a function is declared as const, it can be called on any type of object.
Let – It is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it’s defined in.

What is the difference between Attributes and Property?

Attributes- provide more details on an element like id, type, value etc.

Property- is the value assigned to the property like type=”text”, value=’Name’ etc.

What is the syntax of ‘Self Invoking Function’? Give an example?

How to delete a cookie using JavaScript?

If you want to delete a cookie so that subsequent attempts to read the cookie in JavaScript return nothing, you just need to set the expiration date to a time in the past. You should define the cookie path to ensure that you delete the right cookie. Some browsers will not let you delete a cookie if you don’t specify the path.

What are Self Invoking Functions?

They are also known as ‘Immediately Invoked Function Expressions’ or ‘Self Executing Anonymous Functions’. These functions are invoked automatically in the code, hence they are named as ‘Self Invoking Functions’.

Usually, we define a function and invoke it, but if we want to execute a function automatically where it is explained, and if we are not going to call it again, we can use anonymous functions. And these types of functions have no name.

How to create a cookie using JavaScript?

The simplest way to create a cookie is to assign a string value to the document.cookie object, which looks like this-
Syntax :


document.cookie = "key1 = value1; key2 = value2; expires = date";

Can we break JavaScript Code into several lines? If yes, then How?

Yes, We can break JavaScript code into several lines; we can break within a string statement using a backslash (‘’) at the end of the first line code.

For example, document.write (“This is a program”);

And when you are not within a strong statement and want to change to a new line, then JavaScript ignores the break in the line.

For Example: var x=1, y=2,

z=x+y;

The above code is perfect for better understanding, but it might hamper our debugging, so it is not advisable to write.

What do you mean by negative infinity?

Negative Infinity is nothing but a number in JavaScript that can be derived by dividing negative numbers by zero. This could be generated by arithmetic operations.

Who is faster among JavaScript and ASP Script?

Obviously, JavaScript is faster.

JavaScript is more rapid because, as JS is a client-side language and that it does not need any assistance or help of the webserver to execute, but on the other hand, ASP is a server-side language. That’s why ASP is always slower than JavaScript.

JS now is also known as a server-side language named NodeJS.

What are the variable naming conventions in JavaScript?

The following rules are to be followed while naming variables in JavaScript:

1. You should not use any of the JavaScript reserved keyword as variable name. For example, break or boolean variable names are not valid.
2. JavaScript variable names should not start with a numeral (0-9). They must begin with a letter or the underscore character. For example, 123name is an invalid variable name but _123name or name123 is a valid one.
3. JavaScript variable names are case sensitive. For example, Test and test are two different variables.

What is NaN property in JavaScript?

NaN property represents “Not-a-Number” value. It indicates a value which is not a legal number.

typeof of a NaN will return a Number .

To check if a value is NaN, we use the isNaN() function,

Is javascript a statically typed or a dynamically typed language?

JavaScript is a dynamically typed language. In a dynamically typed language, the type of a variable is checked during run-time in contrast to statically typed language, where the type of a variable is checked during compile-time.Since javascript is a loosely(dynamically) typed language, variables in JS are not associated with any type. A variable can hold the value of any data type.
For example, a variable which is assigned a number type can be converted to a string type:
var a = 23;
var a = "Hello World!";

What is the purpose of ‘This’ operator in JavaScript?

The JavaScript this keyword refers to the object it belongs to. This has different values depending on where it is used. In a method, this refers to the owner object and in a function, this refers to the global object.

What are the scopes of a variable in JavaScript?

The scope of a variable is the region of your program in which it is defined. JavaScript variable will have only two scopes.

Global Variables − A global variable has global scope which means it is visible everywhere in your JavaScript code.
Local Variables − A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.

Can you assign an anonymous function to a variable and pass it as an argument to another function?

Yes! An anonymous function can be assigned to a variable. It can also be passed as an argument to another function.

In case you are facing any challenges with these JavaScript Interview Questions, please comment on your problems in the section below.

Who is faster among JavaScript and ASP Script?

Obviously, JavaScript is faster.

JavaScript is more rapid because, as JS is a client-side language and that it does not need any assistance or help of the webserver to execute, but on the other hand, ASP is a server-side language. That’s why ASP is always slower than JavaScript.

JS now is also known as a server-side language named NodeJS.

What are the pop-ups available in JavaScript?

The popups available in JavaScript are Alert, Prompt, and Confirm.

What is mean by NULL in JavaScript?

If no value has been given to the variable then, it is called a null object (or) null value.

What are the characteristics of JavaScript ‘Strict Mode’?

Given below are the characteristics of ‘Strict Mode’:

‘Strict Mode’ will stop developers from creating global variables.
Developers are restricted from using duplicate parameters.
Strict mode will restrict you from using the JavaScript keyword as a variable name or function name.
Strict mode is declared with ‘use strict’ keyword at the beginning of the script.
All browsers support strict mode.

What is JavaScript ‘Strict Mode’?

‘Strict mode’ is a restricted variant of JavaScript. Usually, this language is ‘not very strict’ in throwing errors. But in ‘Strict mode’ it will throw all types of errors, even the silent errors. Thus, the process of debugging becomes easier. And the chances for making a mistake for the developer is reduced.

Why Should We Study JavaScript?

JavaScript is one of the three languages all web developers must learn:

(1). HTML is used to define the content of web pages. It is otherwise known as the skeleton of web pages.

(2). CSS is used to specify the layout or give styling to the web pages, otherwise known as the shape of the body or cover of the skeleton.

(3). JavaScript to program the behaviour of web pages or web pages workability.

Who developed JavaScript, and what is the first name of JavaScript?

JavaScript was launched in year September 1995.

JavaScript was created by a Netscape programmer, Brendan Eich.

He developed this new scripting language in just ten days.

At the time of launch, it was initially named Mocha, after which it was known as Live Script and later known as JavaScript.

Which Company developed JavaScript?

Netscape company had developed the JavaScript Programming Language.

Name the problems that are associated with the use of global variables?

Even though global variables are easy to use, these have some shortfalls. While using this type of variable, the problem of clashing the variable names of different global and local scope occurs. The code that is often relied on the global variable also gets difficult to be tested and debugged.

What is the definition of global variables? In what way, these variables are declared?

A global variable is a special kind of variable in JavaScript. This variable is easy to use and also available across the entire length of the JavaScript code. Mainly, the var keyword is used whether to declare a global or local variable.

Name the different types of JavaScript data?

JavaScript data are of the following types -

1.String
2.Function
3.Boolean
4.Object
5.Number
6.Null

What are the advantages of JavaScript?

This Scripting language has many advantages as stated below.

Lightweight: It is easy to implement. It has small memory footprints.
Interpreted: It is an interpreted language. Instructions are executed directly.
Object-oriented: It is an object-oriented language.
First-class functions: In JavaScript, a function can be used as a value.
Scripting Language: It’s a language in which instructions are written for a run-time environment.

What is the difference between test () and exec () methods?

Both test () and exec () are RegExp expression methods.

By using a test (), we will search a string for a given pattern, if it finds the matching text then it returns the Boolean value ‘true’ or else it returns ‘false’.

But in exec (), we will search a string for a given pattern, if it finds the matching text then it returns the pattern itself or else it returns ‘null’ value.

Who developed JavaScript, and what is the first name of JavaScript?

JavaScript was launched in year September 1995.

JavaScript was created by a Netscape programmer, Brendan Eich.

He developed this new scripting language in just ten days.

At the time of launch, it was initially named Mocha, after which it was known as Live Script and later known as JavaScript.

What is JavaScript?

JavaScript is a client-side scripting language as well as a server-side scripting language. This scripting language can be written into HTML pages (also could use CSS for styling the pages), and web browsers understand the page.

This scripting language also acts like an object-oriented programming language but not a class-based object-oriented language.

Search
R4R Team
R4R provides JavaScript Freshers questions and answers (JavaScript Interview Questions and Answers) .The questions on R4R.in website is done by expert team! Mock Tests and Practice Papers for prepare yourself.. Mock Tests, Practice Papers,Javascript interview question set 1,JavaScript Freshers & Experienced Interview Questions and Answers,JavaScript Objetive choice questions and answers,JavaScript Multiple choice questions and answers,JavaScript objective, JavaScript questions , JavaScript answers,JavaScript MCQs questions and answers Java, C ,C++, ASP, ASP.net C# ,Struts ,Questions & Answer, Struts2, Ajax, Hibernate, Swing ,JSP , Servlet, J2EE ,Core Java ,Stping, VC++, HTML, DHTML, JAVASCRIPT, VB ,CSS, interview ,questions, and answers, for,experienced, and fresher R4r provides Python,General knowledge(GK),Computer,PHP,SQL,Java,JSP,Android,CSS,Hibernate,Servlets,Spring etc Interview tips for Freshers and Experienced for JavaScript fresher interview questions ,JavaScript Experienced interview questions,JavaScript fresher interview questions and answers ,JavaScript Experienced interview questions and answers,tricky JavaScript queries for interview pdf,complex JavaScript for practice with answers,JavaScript for practice with answers You can search job and get offer latters by studing r4r.in .learn in easy ways .