Javascript



1. What is JavaScript?  

JavaScript is a lightweight, interpreted programming language with object-oriented capabilities that allows you to build interactivity into otherwise static HTML pages.
The general-purpose core of the language has been embedded in Netscape, Internet Explorer, and other web browsers.



2. Name some of the JavaScript features?  

Following are the features of JavaScript -
  • JavaScript is a lightweight, interpreted programming language.
  • JavaScript is designed for creating network-centric applications.
  • JavaScript is complementary to and integrated with Java.
  • JavaScript is is omplementary to and integrated with HTML.
  • JavaScript is open and cross-platform.


3. What are JavaScript types?  

Following are the JavaScript types:
  • Number
  • String
  • Boolean
  • Function
  • Object
  • Null
  • Undefined


4. What are the advantages of using JavaScript?  

Following are the advantages of using JavaScript -
    • Less server interaction - You can validate user input before sending the page off to the server. This saves server traffic, which means less load on your server.
    • Immediate feedback to the visitors - They don't have to wait for a page reload to see if they have forgotten to enter something.
    • Increased interactivity - You can create interfaces that react when the user hovers over them with a mouse or activates them via the keyboard.
    • Richer interfaces - You can use JavaScript to include such items as drag-and-drop components and sliders to give a Rich Interface to your site visitors.



5. What are disadvantages of using JavaScript?  

We can not treat JavaScript as a full fledged programming language. It lacks the following important features -
  • Client-side JavaScript does not allow the reading or writing of files. This has been kept for security reason.
  • JavaScript can not be used for Networking applications because there is no such support available.
  • JavaScript doesn't have any multithreading or multiprocess capabilities.


6. 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 and it is said to be unstructured programming.



7. What is the difference between JavaScript and Jscript?  

Both JavaScript and Jscript are almost similar. JavaScript was developed by Netscape. Microsoft reverse engineered Javascript and called it JScript.



8. Is JavaScript a case-sensitive language?  

Yes! JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.

A function getElementById is not the same as getElementbyID.



9. How do we add JavaScript onto a web page?  

There are several way for adding JavaScript on a web page, but there are two ways which are commonly used by developers
If your script code is very short and only for single page, then following ways are the best

(i) You can place <script type="text/javascript"> tag inside the <head> element.
              <head>
              <title>Page Title </title>
              <script language="JavaScript" type="text/javascript">
                  var name = "Vikash Kumar"
                  alert(name);
              </script>
              </head>     
                   


(ii) If your script code is very large, then you can make a JavaScript file and add its path in the following way:
              <head>
              <title>Page Title </title>
              <script type="text/javascript" src="myjavascript.js"></script>
              </head> 
              


10. What is the use of isNaN function?  

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



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



12. What is the difference between "==" and "==="?  

"==" checks equality only,
"===" checks for equality as well as the type.



13. How to set a default parameter value for a JavaScript function?  

             /** Here email is parameter in which we have set the default value i.e email@domain.com **/
             function function1(name, email)
              {
                email = typeof email !== 'undefined' ? email : 'defaultemail@domain.com';
                 console.log('name='+name+', Email= '+email);
              }

             function1('vikash','myname@gmail.com');
             function1('vikash');
             


14. How can you create an Object in JavaScript?  

JavaScript supports Object concept very well. You can create an object using the object literal as follows -
              var emp = {
                 name: "Vikash",
                 age: 10
              };
              


15. Is it possible to break JavaScript Code into several lines?  

Breaking within a string statement can be done by the use of a backslash, ", at the end of the first line
Example:
 document.write("This is \a program");  

And if you change to a new line when not within a string statement, then javaScript ignores break in line.
Example:
              var x=1, y=2,
 
              z=
 
              x+y;

The above code is perfectly fine,though not advisible as it hampers debugging.


16. 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.



17. Write the code for adding new elements dynamically?  

           <p><html> 
           <head> <title>t1</title> 
           <script type="text/javascript"> 
           function addNode() { var newP = document.createElement("p"); 
           var textNode = document.createTextNode(" This is a new text node"); 
           newP.appendChild(textNode); document.getElementById("firstP").appendChild(newP); } 
           </script> </head> 
           <body> <p id="firstP">firstP <p> </body> 
           </html> 
           


18. 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.
Example:

// 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.



19. 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
           emp.name  // ==> Zara
           emp.age   // ==> 10
           // Setting object properties
           emp.name = "Daisy"  // <== Daisy
           emp.age  =  20      // <== 20
           


20. How to convert a string to lowercase?  

         var str='This is testing String';
         str = str.toLowerCase();
         console.log(str);
         


Java Interview Question

.Net Interview Question

PHP Interview Question

AngularJS Interview Questions