JavaScript

1) What is JavaScript?

Ans)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

Ans) 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 complementary to and integrated with HTML.
  • JavaScript is open and cross-platform.

  • 3) What are the advantages of using JavaScript?

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

  • 4)Is JavaScript a case-sensitive language?

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


    5)How can you create an Object in JavaScript?

    Ans) JavaScript supports Object concept very well. You can create an object using the object literal as follows −

    var emp = { name: "Zara", age: 10 };


    6) How can you read properties of an Object in JavaScript?

    Ans) // Getting object properties emp.name // ==> Zara emp.age // ==> 10 // Setting object properties emp.name = "Daisy" // <== Daisy emp.age = 20 // <== 20


    7)How can you create an Array in JavaScript?

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


    8) How to read elements of an array in JavaScript?

    Ans)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] }


    9)What is a named function in JavaScript? How to define a named function?

    Ans)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 }


    10) How many types of functions JavaScript supports?

    Ans)A function in JavaScript can be either named or anonymous.


    11)How to define a anonymous function?

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


    12)Can you assign a anonymous function to a variable?

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


    13)Can you pass a anonymous function as an argument to another function?

    Ans)Yes! An anonymous function can be passed as an argument to another function.


    14)What is arguments object in JavaScript?

    Ans)JavaScript variable arguments represents the arguments passed to a function.


    15) How can you get the type of arguments passed to a function?

    Ans) Using typeof operator, we can get the type 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


    16)How can you get the total number of arguments passed to a function?

    Ans) 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


    17) How can you get the reference of a caller function inside a function?

    Ans)The arguments object has a callee property, which refers to the function you're inside of. For example − function func() { return arguments.callee; } func(); // ==> func


    18) What is the purpose of 'this' operator in JavaScript?

    Ans) JavaScript famous keyword this always refers to the current context.


    19)What are the valid scopes of a variable in JavaScript?

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

  • 20) What is the difference between undefined and not defined in JavaScript?

    Ans)In JavaScript, if you try to use a variable that doesn't exist and has not been declared, then JavaScript will throw an error var name is not defined and script will stop executing. However, if you use typeof undeclared_variable, then it will return undefined.


    21)What is callback?

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


    22)What is closure?

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


    23)Which built-in method returns the character at the specified index?

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


    24)Which built-in method combines the text of two strings and returns a new string?

    Ans)concat() method returns the character at the specified index.


    25)Which built-in method calls a function for each element in the array?

    Ans)forEach() method calls a function for each element in the array.


    26)What is the difference between JavaScript and jscript?

    Ans)Netscape provided the JavaScript language. Microsoft changed the name and called it JScript to avoid the trademark issue. In other words, you can say JScript is same as JavaScript, but it is provided by Microsoft.


    27)How to use external JavaScript file?

    Ans)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>

    28)How to create function in JavaScript?

    Ans)To create function in JavaScript, follow the following syntax. function function_name(){ //function body }

    29) What is the difference between == and ===?

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


    30) How to write html code dynamically using JavaScript?

    Ans) 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>";

    31)How to create objects in JavaScript?

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

    32) How to create array in JavaScript?

    Ans) There are 3 ways to create array in JavaScript. 1.By array literal 2.By creating instance of Array 3.By using an Array constructor 4.Let's see a simple code to create array using object literal. var emp=["Shyam","Vimal","Ratan"]; .

    33)What does the isNaN() function?

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


    34)Difference between Client side JavaScript and Server side JavaScript?

    Ans)Difference is:

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

  • 35)In which location cookies are stored on the hard disk?

    Ans) The storage of cookies on the hard disk depends on OS and the browser. The Netscape navigator on Windows uses cookies.txt file that contains all the cookies. The path is : c:\Program Files\Netscape\Users\username\cookies.txt The Internet Explorer stores the cookies on a file username@website.txt. The path is: c:\Windows\Cookies\username@Website.txt.


    36)What is the difference between undefined value and null value?

    Ans) Undefined value: A value that is not defined and has no keyword is known as undefined value. For example: int 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: String str=null;//Here, str has a null value. </p> <br/> <p><b>37)How to set the cursor to wait in JavaScript?</b> Ans)The cursor can be set to wait in JavaScript by using the property "cursor". The following example illustrates the usage: <script> window.document.body.style.cursor = "wait"; </script>

    38)How to submit a form using JavaScript by clicking a link?

    Ans) 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> </form> <script type="text/javascript"> function submitform() { document.myform.submit(); } </script>

    39) How to change the background color of HTML document using JavaScript?

    Ans) <script type="text/javascript"> document.body.bgColor="pink"; </script>

    40)How to handle exceptions in JavaScript?

    Ans) By the help of try/catch block, we can handle exceptions in JavaScript. JavaScript supports try, catch, finally and throw keywords for exception handling.


    41)What is the output of "10"+20+30 in JavaScript?

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


    42)What is the output of 10+20+"30" in JavaScript?

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


    43)Explain how to read and write a file using JavaScript?

    Ans) There are two ways to read and write a file using JavaScript

  • Using JavaScript extensions
  • Using a web page and Active X objects

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

    Ans) The parseInt() function is used to convert numbers between different bases. parseInt() takes the string to be converted as its first parameter, and the second parameter is the base of the given string. In order to convert 4F (of base 16) to integer, the code used will be – JavaScript parseInt ("4F", 16); parseInt ("4F", 16);

    45)What do mean by NULL in Javascript?

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


    46)What is the use of Void(0)?

    Ans)Void(0) is used to prevent the page from refreshing and parameter “zero” is passed while calling.

    Void(0) is used to call another method without refreshing the page


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

    Ans) An alert box displays only one button which is the OK button. But a Confirmation box displays two buttons namely OK and cancel.


    48)Explain what is pop()method in JavaScript?

    Ans) 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. Example: var cloths = [“Shirt”, “Pant”, “TShirt”]; cloths.pop(); //Now cloth becomes Shirt,Pant

    49)Whether JavaScript has concept level scope?

    Ans)No. JavaScript does not have concept level scope. The variable declared inside the function has scope inside the function.


    50)What is break and continue statements?

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