Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Thursday, October 4, 2018

JavaScript Related Questions & Answers

1.What is JavaScript?

JavaScript is a client-side as well as server side scripting language that can be inserted into HTML pages and is understood by web browsers. JavaScript is also an Object based Programming language

2.Enumerate the differences between Java and JavaScript?

Java is a complete programming language. In contrast, JavaScript is a coded program that can be introduced to HTML pages. These two languages are not at all inter-dependent and are designed for the different intent.  Java is an object – oriented programming (OOPS) or structured programming language like C++ or C whereas JavaScript is a client-side scripting language.

3.What are JavaScript Data Types?

Following are the JavaScript Data types:
·         Number
·         String
·         Boolean
·         Null
·         Object
·         Undefined
·         Array

4.What is the use of isNaN function?

isNaN function returns true if the argument is not a number otherwise it is false.

5.Between JavaScript and an ASP script, which is faster?

JavaScript is faster. JavaScript is a client-side language and thus it does not need the assistance of the web server to execute. On the other hand, ASP is a server-side language and hence is always slower than JavaScript.  Javascript now is also a server side language (nodejs).

6.Which company developed JavaScript?

Netscape is the software company who developed JavaScript.

7.What are undeclared and undefined variables?

Undeclared variables are those that do not exist in a program and are not declared. If the program tries to read the value of an undeclared variable, then a runtime error is encountered.
Undefined variables are those that are declared in the program but have not been given any value. If the program tries to read the value of an undefined variable, an undefined value is returned.

8.What is callback?

A callback is a plain JavaScript function passed to some method as an argument or option. Some callbacks are just events, called to give the user a chance to react when a certain state is triggered.

9.How to create an object?

An object in JavaScript can be created using two was:
New Key word:
To create a student object from the above student class we can call the Student function using new keyword.
var student1 = new Student(‘santosh’,2)
Anonymous Object:
Anonymous objects can be created using pair of curls’ braces containing property name and value pairs.
Var rose = {‘color’: ‘red’}

10. What are global variables? How are these variable declared and what are the problems associated with using them?

Global variables are those that are available throughout the length of the code, that is, these have no scope. The var keyword is used to declare a local variable or object. If the var keyword is omitted, a global variable is declared.
// Declare a global globalVariable = “Test”;
The problems that are faced by using global variables are the clash of variable names of local and global scope. Also, it is difficult to debug and test the code that relies on global variables.

11. What is a prompt box?

A prompt box is a box which allows the user to enter input by providing a text box.  Label and box will be provided to enter the text or number.

12. What is ‘this’ keyword in JavaScript?

‘This’ keyword refers to the object from where it was called.

13. Explain the working of timers in JavaScript? Also elucidate the drawbacks of using the timer, if any?

Timers are used to execute a piece of code at a set time or also to repeat the code in a given interval of time. This is done by using the functions setTimeout, setInterval and clearInterval.
The setTimeout(function, delay) function is used to start a timer that calls a particular function after the mentioned delay. The setInterval(function, delay) function is used to repeatedly execute the given function in the mentioned delay and only halts when cancelled. The clearInterval(id) function instructs the timer to stop.
Timers are operated within a single thread, and thus events might queue up, waiting to be executed.

14. Which symbol is used for comments in Javascript?

// for Single line comments and
/*   Multi

15. What is === operator?

=== is called as strict equality operator which returns true when the two operands are having the same value without any type conversion.

16. Explain how can you submit a form using JavaScript?

To submit a form using JavaScript use document.form[0].submit();

17. How can the style/class of an element be changed?

It can be done in the following way:
document.getElementById(“myText”).style.fontSize = “20″;
document.getElementById(“myText”).className = “anyclass”;

18. Explain the difference between “==” and “===”?

“==” checks only for equality in value whereas “===” is a stricter equality test and returns false if either the value or the type of the two variables are different.

19. What would be the result of 3+2+”7″?

Since 3 and 2 are integers, they will be added numerically. And since 7 is a string, its concatenation will be done. So the result would be 57.

20. Explain how to detect the operating system on the client machine?

In order to detect the operating system on the client machine, the navigator.appVersion string (property) should be used.

21. What do mean by NULL in Javascript?

The NULL value is used to represent no value or no object.  It implies no object or null string, no valid boolean value, no number and no array object.

22. What is the function of delete operator?

The functionality of delete operator is used to delete all variables and objects in a program but it cannot delete variables declared with VAR keyword.

23. What is an undefined value in JavaScript?

Undefined value means the
·         Variable used in the code doesn’t exist
·         Variable is not assigned to any value
·         Property doesn’t exist

24. Explain what is pop()method in JavaScript?

The pop() method is similar as the shift() method but the difference is that the Shift method works at the start of the array.  Also the pop() method take the last element off of the given array and returns it. The array on which is called is then altered.
var cloths = [“Shirt”, “Pant”, “TShirt”];
//Now cloth becomes Shirt,Pant

25. What is break and continue statements?

Break statement exits from the current loop.
Continue statement continues with next statement of the loop.

26. What is the use of type of operator?

‘Typeof’ is an operator which is used to return a string description of the type of a variable.

27. What is the use of Push method in JavaScript?

The push method is used to add or append one or more elements to the end of an Array. Using this method, we can append multiple elements by passing multiple arguments.

28. What is unshift method in JavaScript?

Unshift method is like push method which works at the beginning of the array.  This method is used to prepend one or more elements to the beginning of the array.

29. What is the ‘Strict’ mode in JavaScript and how can it be enabled?

Strict Mode adds certain compulsions to JavaScript. Under the strict mode, JavaScript shows errors for a piece of codes, which did not show an error before, but might be problematic and potentially unsafe. Strict mode also solves some mistakes that hamper the JavaScript engines to work efficiently.
Strict mode can be enabled by adding the string literal “use strict” above the file.

30. What is the way to get the status of a CheckBox?

The status can be acquired as follows –
If the CheckBox will be checked, this alert will return TRUE.

31. Describe the properties of an anonymous function in JavaScript?

A function that is declared without any named identifier is known as an anonymous function. In general, an anonymous function is inaccessible after its declaration.

32. Is JavaScript case sensitive? Give an example?

Yes, JavaScript is case sensitive. For example, a function parseInt is not same as the function Parseint.

33. What boolean operators can be used in JavaScript?

The ‘And’ Operator (&&), ‘Or’  Operator (||) and the ‘Not’ Operator (!) can be used in JavaScript.
*Operators are without the parenthesis.

34. What is the role of break and continue statements?

Break statement is used to come out of the current loop while the continue statement continues the current loop with a new recurrence.

35. How are DOM utilized in JavaScript?

DOM stands for Document Object Model and is responsible for how various objects in a document interact with each other. DOM is required for developing web pages, which includes objects like paragraph, links, etc. These objects can be operated to include actions like add or delete. DOM is also required to add extra capabilities to a web page. On top of that, the use of API gives an advantage over other existing models.

36. Explain the unshift() method ?

This method is functional at the starting of the array, unlike the push(). It adds the desired number of elements to the top of an array.

37. How to use external JavaScript file?

I am assuming that js file name is message.js, place the following script tag inside the head tag.
<script type=”text/javascript” src=”message.js”></script>  

38. What is DOM? What is the use of document object?

DOM stands for Document Object Model. A document object represent the html document. It can be used to access and change the content of html.

39. What is the use of window object?

The window object is automatically created by the browser that represents a window of a browser.
It is used to display the popup dialog box such as alert dialog box, confirm dialog box, input dialog box etc.

40. What is the use of history object?

The history object of browser can be used to switch to history pages such as back and forward from current page or another page. There are three methods of history object.
1.    back()
2.    forward()
3.    go(number): number may be positive for forward, negative for backward.

41. How to write comment in JavaScript?

There are two types of comments in JavaScript.
1.    Single Line Comment: It is represented by // (double forward slash)
2.    Multi Line Comment: It is represented by slash with asterisk symbol as /* write comment here */

42. What is the difference between == and ===?

The == operator checks equality only whereas === checks equality and data type i.e. value must be of same type.

43. How to write html code dynamically using JavaScript?

The innerHTML property is used to write the HTML code using JavaScript dynamically. Let’s see a simple example:
document.getElementById(‘mylocation’).innerHTML=”<h2>This is heading using JavaScript</h2>”;   

44. How to write normal text code using JavaScript dynamically?

The innerText property is used to write the simple text using JavaScript dynamically. Let’s see a simple example:
document.getElementById(‘mylocation’).innerText=”This is text using JavaScript”;   

45. How to create objects in JavaScript?

There are 3 ways to create object in JavaScript.
1.    By object literal
2.    By creating instance of Object
3.    By Object Constructor
Let’s see a simple code to create object using object literal.
emp={id:102,name:”Rahul Kumar”,salary:50000}   

46. What does the isNaN() function?

The isNaN() function returns true if the variable value is not a number.

47. What is the output of 10+20+”30″ in JavaScript?

3030 because 10+20 will be 30. If there is numeric value before and after +, it is treated is binary + (arithmetic operator).

48. What is the output of “10”+20+30 in JavaScript?

102030 because after a string all the + will be treated as string concatenation operator (not binary +).

49. Difference between Client side JavaScript and Server side JavaScript?

Client side JavaScript comprises the basic language and predefined objects which are relevant to running java script in a browser. The client side JavaScript is embedded directly by in the HTML pages. This script is interpreted by the browser at run time.
Server side JavaScript also resembles like client side java script. It has relevant java script which is to run in a server. The server side JavaScript are deployed only after compilation.

50. What is the real name of JavaScript?

The original name was Mocha, a name chosen by Marc Andreessen, founder of Netscape. In September of 1995, the name was changed to LiveScript. In December 1995, after receiving a trademark license from Sun, the name JavaScript was adopted.

51. What is the difference between undefined value and null value?

Undefined value: A value that is not defined and has no keyword is known as undefined value. For example:
var number;//Here, number has undefined value.  
Null value: A value that is explicitly specified by the keyword “null” is known as null value. For example:
var str=null;//Here, str has a null value.  

52. How to submit a form using JavaScript by clicking a link?

Let’s see the JavaScript code to submit form on clicking the link.
<form name=”myform” action=”index.php”>  
Search: <input type=’text’ name=’query’ />  
<a href=”javascript: submitform()”>Search</a>  
<script type=”text/javascript”>  
function submitform()  

53. How to change the background color of HTML document using JavaScript?

<script type=”text/javascript”>  

54. How can you read properties of an Object in JavaScript?

You can write and read properties of an object using the dot notation as follows −
Getting object properties  // ==> Zara
emp.age   // ==> 10
Setting object properties = “Daisy”  // <== Daisy
emp.age  =  20      // <== 20

55. How can you create an Array in JavaScript?

You can define arrays using the array literal as follows −
var x = [];
var y = [1, 2, 3, 4, 5];

56. How to read elements of an array in JavaScript?

An array has a length property that is useful for iteration. We can read elements of an array as follows −
var x = [1, 2, 3, 4, 5];
for (var i = 0; i < x.length; i++) {
   // Do something with x[i]

57. What is a named function in JavaScript? How to define a named function?

A named function has a name when it is defined. A named function can be defined using function keyword as follows −
function named(){
   // do some stuff here

58. How to define a anonymous function?

An anonymous function can be defined in similar way as a normal function but it would not have any name.

59. Can you assign a anonymous function to a variable?

Yes! An anonymous function can be assigned to a variable.

60. What is the purpose of ‘this’ operator in JavaScript?

JavaScript famous keyword this always refers to the current context.

61. What are the valid 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.

62. What is callback?

A callback is a plain JavaScript function passed to some method as an argument or option. Some callbacks are just events, called to give the user a chance to react when a certain state is triggered.

63. Which built-in method returns the character at the specified index?

charAt() method returns the character at the specified index.

64. How typeof operator works?

The typeof is a unary operator that is placed before its single operand, which can be of any type. Its value is a string indicating the data type of the operand.
The typeof operator evaluates to “number”, “string”, or “boolean” if its operand is a number, string, or boolean value and returns true or false based on the evaluation.

65. How to redirect a url using JavaScript?

This is very simple to do a page redirect using JavaScript at client side. To redirect your site visitors to a new page, you just need to add a line in your head section as follows −
<script type=”text/javascript”>

66. What is Date object in JavaScript?

The Date object is a datatype built into the JavaScript language. Date objects are created with the new Date( ).
Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC (universal, or GMT) time.

67. What is Number object in JavaScript?

The Number object represents numerical date, either integers or floating-point numbers. In general, you do not need to worry about Number objects because the browser automatically converts number literals to instances of the number class.
Syntax −
Creating a number object −
var val = new Number(number);
If the argument cannot be converted into a number, it returns NaN (Not-a-Number).Enq

JavaScript Related Questions & Answers

1.What is JavaScript? JavaScript is a client-side as well as server side scripting language that can be inserted into HTML pages an...