JavaScript HelpDesk

  • 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 Oriented Programming language.
  • 2. What's 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.
  • 3. What are JavaScript types?

    Following are the JavaScript types:

    • Number
    • String
    • Boolean
    • Function
    • Object
    • Null
    • Undefined

  • 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. What is negative infinity?
    Negative Infinity is a number in JavaScript which can be derived by dividing negative number by zero.
    
  • 7. 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");
  • 8. Which company developed JavaScript?

    Netscape is the software company who developed JavaScript.

  • 9. What 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.

  • 10. 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.

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

  • 12. 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.

  • 13. 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.

  • 14. 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.

  • 15. 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: "Sunil Guleria",
       age: 38
    };
    
  • 16. What is 'this' keyword in JavaScript?

    'this' keyword refers to the object from where it was called.

  • 17. 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.

  • 18. 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];
    
  • 19. 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]
    }
    
  • 20. 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
    }
    
  • 21. Which symbol is used for comments in Javascript?
    // for Single line comments and

    /*   Multi
    	
    	Line
    
    Comment
    
    */
    
  • 22. 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.

  • 23. Explain how can you submit a form using JavaScript?

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

    document.form[0].submit();

  • 24. How many types of functions JavaScript supports?

    A function in JavaScript can be either named or anonymous.

  • 25. What is arguments object in JavaScript?.

    JavaScript variable arguments represents the arguments passed to a function.

  • 26.How can you get the total number of arguments passed to a function?

    Using arguments.length property, we can get the total number of arguments passed to a function. For example −:

    function func(x){
       console.log(typeof x, arguments.length);
    }
    func();                //==> "undefined", 0
    func(1);               //==> "number", 1
    func("1", "2", "3");   //==> "string", 3
    
  • 27. 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.

  • 28. What is closure?

    Closures are created whenever a variable that is defined outside the current scope is accessed from within some inner scope.

  • 29. Does JavaScript support automatic type conversion?

    Yes JavaScript does support automatic type conversion, it is the common way of type conversion used by JavaScript developers

  • 30. What are all the looping structures in JavaScript?.

    Following are looping structures in Javascript:

    • For
    • While
    • do-while loops

  • 31. 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.

  • 32. Can you access Cookie using javascript?

    JavaScript can also manipulate cookies using the cookie property of the Document object. JavaScript can read, create, modify, and delete the cookie or cookies that apply to the current web page.

  • 33. 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";
    
  • 34. 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 −

    <head>
    <script type="text/javascript">
    <!--
       window.location="http://www.newlocation.com";
    //-->
    </script>
    </head>
    
  • 35. 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.

  • 36. 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.

  • 37. 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.

  • 38.What is the data type of variables of in JavaScript?

    All variables in the JavaScript are object data types.

  • 39. How to print a web page using javascript?

    JavaScript helps you to implement this functionality using print function of window object. The JavaScript print function window.print() will print the current web page when executed.

  • 40. 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.

  • 41. How to handle exceptions in JavaScript?

    The latest versions of JavaScript added exception handling capabilities. JavaScript implements the try...catch...finally construct as well as the throw operator to handle exceptions.

    You can catch programmer-generated and runtime exceptions, but you cannot catch JavaScript syntax errors.

  • 42. What is purpose of onError event handler in JavaScript?

    The onerror event handler was the first feature to facilitate error handling for JavaScript. The error event is fired on the window object whenever an exception occurs on the page.

    The onerror event handler provides three pieces of information to identify the exact nature of the error −

    Error message − The same message that the browser would display for the given error.

    URL − The file in which the error occurred.

    Line number −The line number in the given URL that caused the error.

  • 43. What is the way to get the status of a CheckBox?
    alert(document.getElementById('checkbox1').checked);
    

    If the CheckBox will be checked, this alert will return TRUE.

  • 44. Explain the for-in loop?

    The for-in loop is used to loop through the properties of an object.

    for (variable name in object){
     
    statement or block to execute
    }
    
  • 45. Explain window.onload and onDocumentReady?

    The onload function is not run until all the information on the page is loaded. This leads to a substantial delay before any code is executed.

    onDocumentReady loads the code just after the DOM is loaded. This allows early manipulation of the code.

  • 46. How to read a Cookie using JavaScript?

    Reading a cookie is just as simple as writing one, because the value of the document.cookie object is the cookie. So you can use this string whenever you want to access the cookie.

    The document.cookie string will keep a list of name = value pairs separated by semicolons, where name is the name of a cookie and value is its string value.

    You can use string split() function to break the string into key and values.