Wednesday, 28 August 2019

Basics Java Script questions need to know

Q1) What is JavaScript?

Ans: JavaScript is a scripting language most often used for client-side web development. there are two ways which are normally utilized by developers.

JavaScript is a lightweight, translated programming language with object-oriented capacities that allow you to incorporate intelligence with generally static HTML pages.

i) <script type="text/javascript"> tag inside the <head> element.

<head>
<title>Java script </title>
<script language="JavaScript" type="text/javascript">
  var name = "kumar "
  alert(name);
</script>
</head>

ii) If your script code is very large, then you can make a JavaScript file and include its way in the following way:

<head>
<title>javascript</title>
<script type="text/javascript" src="script.js"></script>
</head>


Q2) What is the difference between an alert box and a confirmation box?

Alert box shows one and only button which is the OK button while the Confirm box shows two buttons namely OK and cancel.

Q3) What is a prompt box?

A prompt box allows the user to enter input by providing a text box.

Q4) what are the datatype in JavaScript?

  String,Number,Boolean,Array,Object,Null,Undefined.

Q5) Javascript case sensitive?

 Yes!, absolutely Javascript is case sensitive

 i) The function getElementById is not the same as getElementbyID
 ii) var NAME=”kumar”; is not the same var name=”rangari”;

Q6) what is the difference between ‘==’ and ‘===’?

‘==’ checks for value equality,
Equality using "=="

var num = 0;
var obj = new String("0");
var str = "0";
var b = false;
print(num == num); // true
print(obj == obj); // true
print(str == str); // true
print(num == obj); // true
print(num == str); // true

‘===’ checks for both type and value.
Strict equality using ===

var num = 0;
var obj = new String("0");
var str = "0";
var b = false;
print(num === num); // true
print(obj === obj); // true
print(str === str); // true
print(num === obj); // false
print(num === str); // false


Q7) What is negative infinity?

It’s a number in JavaScript, derived by dividing negative number by zero.

Q8) Can you explain what isNaN function does?

isNaN function will check an argument and return TRUE if the argument does not seem to be a number.

Return true if the argument is not a number.

Q9) what is the different between write() vs writeln()?

i) document.write();
basic JavaScript commands is document.write. This simply prints the specified text to the page. To print message truly, enter the text in single quote marks inside parentheses like so:

document.write('Hello World!');
The code above will cause the phrase "Hello World!" to appear on the page.

You can also use document.write to print variables. Enter the variable name without quotes, like so:

var mytext = "Hello again";
document.write(mytext);

Note that if quote marks are placed around the variable name, the variable name itself will be printed (instead of the variable value). You can also combine variable values and text strings by using the + sign:

var colour1 = "blue";
var colour2 = "red";
document.write('<p>colour1: ' + colour1 + '<br>colour2: ' + colour2 + '</p>');

Notice the way the variable names are used literally as well as for passing the values. This will print the following:

colour1: blue
colour2: red
Remember, text inside quotes will be printed literally, text with no quotes is assumed to be a variable.

write() vs writeln()
--------------------

write():

Note that write() does NOT add a new line after each statementex: Hello World! Have a nice day!

writeln()

Note that writeln() add a new line after each statement:Hello World!

Have a nice day!


Q10) What are the  boolean operators does  JavaScript support?

&& – the “and” operator.
|| – the “or” operator.
 ! – the “not” operator.


Q11) How to create arrays in JavaScript?

Ans:There are two ways to create array in JavaScript like other languages:

  a) The first way to create array:  By Declare Array:

var names = new Array();

Add Elements in Array:-
names[0] = "jehanabad";
names[1] = "nishant";
names[2] = "kumar";

  b) This is the second way:

var names = new Array("jehanabad", "nishant", "kumar");


Q12) What is the difference between undefined and null?

The value of a variable with no value is undefined (i.e., it has not been initialized).

Var =a;
a is undefined

Variables can be emptied by setting their value to null.

Var a =null;
a is null


Q13) What is the HTML DOM?

Once a site page loads, your browser generates something called a DOM,, or Document Object Model, of the page. The DOM acts as programming interface for HTML, which defines HTML properties, events, and methods. It likewise refers to HTML elements as objects..

JavaScript relies on this DOM to alter the elements and attributes of a page, and create the dynamic websites

demonstrating the hierarchy of HTML DOM objects:

<document>
 
  <html>

     <head>
         <title> </title>
     </head>

     <body>
       -----
     </body>

  </html>

</document>


Q14) What are global variables? How are they declared? What are the problems with using globals?


Global variables are available throughout your code: that is, the variables have no scope.

Local variables scope, then again, is restricted to where it is declared (like within a function).

The var keyword is used to declare  a local  variable or object, while discarding the var keywordcreates a global variable.

Most JavaScript developers avoid globals. One reason why is they’re averse to naming conflicts between local and globals,

Also, code that depends on globals can be difficult to maintain and test.

local variable
var localVariable = "local"

global variable
globalVariable = "global"


Q15) Difference between window.onload and onDocumentReady?

The onload event does not fire until every last piece of the page is loaded, this includes css and images, which means there’s a huge delay before any code is executed.

That isnt what we want. We just want to wait until the DOM is loaded and is able to be manipulated. onDocumentReady allows the programmer to do that.


Q16) 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.


Q17) What are Javascript closures?When would you use them?

closure is the local variables for a function – kept alive after the function has returned,
closure is a stack-frame which is not deallocated when the function returns.
closure takes place when a function creates an environment that binds local variables to it in such a way that they are kept alive after the function has returned.
closure is a special kind of object that combines two things: a function, and any local variables that were in-scope at the time that the closure was created.

function sayHello2(name) {
var text = ‘Hello ‘ + name; // local variable
var sayAlert = function() { alert(text); }
return sayAlert;
}

Closures reduce the need to pass state around the application. The inner function has accessto the variables in the external function so there is no need to  to store the information some place that the inner function can get it.

This is important when the inner function will be called after the function has exited. The most common example of this is when the inner function is being used to handle an event. In this case you get no control over the arguments that are passed to the function so using a closure to keep track of state can be very convenient.


Q18) How to determine the state of a checkbox using Javascript?

var checkedP = window.document.getElementById("myCheckBox").checked;

Q19) What does "7"+5+6 evaluate to?

Ans: Since 7 is a string, everything is a string, so the result is 756.

Q20) What is “this” keyword?

Ans. It refers to the current object.

No comments:

Post a Comment

JSP interview questions and answers

Q1. What is JSP and why do we need it? JSP stands for JavaServer Pages. JSP is java server side technology to create dynamic web pages. J...