Wednesday, 19 April 2017

JavaScript Interview Questions & Answers PART-1

(1) What is JavaScript?
=> JavaScript is a client-side as well as server side scripting language.
=> 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) What is the difference between Java & JavaScript?
Java
JavaScript
Java is an OOP programming language.
JavaScript is an OOP scripting language.
It creates applications that run in a virtual machine or browser.
The code is run on a browser and V8 engine.
Java code needs to be compiled.
JavaScript code no need to be compiled.

 (3) What are the data types supported by JavaScript?
 The data types supported by JavaScript are:
  1.Undefined   2.Null   3.Boolean   4.String   5.Symbol   6.Number   7.Object

(4) What are the JavaScript features?
 => It is a lightweight, interpreted programming language.
 => It is designed for creating network-centric applications.
 => It is complementary to and integrated with Java & HTML.
 => It is open and cross-platform scripting language.

(5) 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.

(6) What are the advantages of 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.

(7) What are disadvantages of using JavaScript?
We cannot 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 cannot be used for networking applications because there is no such support available.
=> JavaScript doesn't have any multithreading or multiprocessor capabilities.

(8) What is the difference between JavaScript and Jscript? 
Both are almost similar. JavaScript is developed by Netscape and Jscript was developed by Microsoft.

(9) What are the 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.

(10) 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.

(11) Which company developed JavaScript?
Netscape is the software company who developed JavaScript.

(12) Which symbol is used for comments in Javascript?
// for Single line comments and
/*   Multi Line Comment */

(13) What is the difference between “==” and “===”? 
“==” checks equality/ only compares values, 
“===” checks for equality/compare values as well as the type.

(14) Difference between “var” and “let” Keywords?
Var was there from the beginning but the let was introduced in ES2015/ES6.
Let has block scope and “Var” has function scope.

(15) How do you change the style/class on any element using JavaScript? 
document.getElementById(“myText”).style.fontSize = “10";
-or-
document.getElementById(“myText”).className = “anyclass”;

(16) What are all the looping structures in JavaScript?












Following are looping structures in JavaScript















For 















While














do-while


























(17) Explain how can you submit a form using JavaScript?
To submit a form using JavaScript use document.form[0].submit();

(18) Explain the for-in loop?
The for-in loop is used to loop through the properties of an object.
The syntax for the for-in loop is –
for (variable name in object){
statement or block to execute
}

(19) 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];

(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(){
   // write code here
}

(21) Can you assign an anonymous function to a variable?
Yes! An anonymous function can be assigned to a variable.

(22) Can you pass an anonymous function as an argument to another function?
Yes! An anonymous function can be passed as an argument to another function.

(23) 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.

(24) What is Callback?
A callback is a plain JavaScript function passed to some method as an argument or option. It is a function that is to be executed after another function has finished executing, hence the name ‘call back‘. In JavaScript, functions are objects. Because of this, functions can take functions as arguments, and can be returned by other functions.

(25) What is Closure? Give an example.
Closures are created whenever a variable that is defined outside the current scope is accessed from within some inner scope.
Example of closure
How the variable counter is visible within the create, increment, and print functions, but not outside of them −
function create() {
   var counter = 0;
   return {
      increment: function() {
         counter++;
      },
      print: function() {
         console.log(counter);
      }
   }
}
var c = create();
c.increment();
c.print();     // ==> 1

It gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created. To use a closure, simply define a function inside another function and expose it.
Name some of the built-in methods and the values returned by them.

Built-in Method
Values
CharAt()
It returns the character at the specified index.
Concat()
It joins two or more strings.
forEach()
It calls a function for each element in the array.
indexOf()
It returns the index within the calling String object of the first occurrence of the specified
value, or −1 if not found.
length()
It returns the length of the string.
pop()
It removes the last element from an array and returns that element.
push()
It adds one or more elements to the end of an array and returns the new length of the array.
reverse()
It reverses the order of the elements of an array.
sort()
It sorts the elements of an array.

(26) What is the purpose of ‘This’ operator in JavaScript?
JavaScript famous keyword this always refers to the current context.
The JavaScript this keyword refers to the object it belongs to. This has different values depending on where it is used. In a method, this refers to the owner object and in a function; this refers to the global object.          

(27) How are object properties assigned?
Properties are assigned to objects in the following way – obj[“class”]=12; 
or obj.class=12;

(28) 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

(29) What is the use of Math Object in JavaScript? 
 The math object provides you properties and methods for mathematical constants and functions.
ex:- var x = Math.PI; // Returns PI
var y = Math.sqrt(16); // Returns the square root of 16
var z = Math.sin(90);    Returns the sine of 90

(30) How you will add function as a property in a JavaScript object? Give an example. 
var man = new Object();
man.name = 'Vikas Ahlawat';
man.living = true;
man.age = 27;
man.getName = function() { return man.name;}
console.log(man.getName()); // Logs 'Vikas Ahlawat'.

(31) 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: "Zara",
   age: 10
};

(32) What is an object in JavaScript, give an example?
 An object is just a container for a collection of named values:
// Create the man object
var man = new Object();
man.name = 'Vikas Ahlawat';
man.living = true;
man.age = 27;

(33) 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]
}

(34) How many types of functions JavaScript supports?
A function in JavaScript can be either named or anonymous.

(35) How to define an anonymous function?
An anonymous function can be defined in similar way as a normal function but it would not have any name.

(36) What is arguments object in JavaScript?
JavaScript variable arguments represents the arguments passed to a function.

(37) How can you get the type of arguments passed to a function?
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

(38) 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

(39) How can you get the reference of a caller function inside a function?
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

(40)  Which type of variable among global and local, takes precedence over other if names are same?
A local variable takes precedence over a global variable with the same name.

(41) 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.

(42)  Which built-in method reverses the order of the elements of an array?
reverse() method reverses the order of the elements of an array −− the first becomes the last, and the last becomes the first.

(43) Which built-in method returns the characters in a string beginning at the specified location?
substr() method returns the characters in a string beginning at the specified location through the specified number of characters.

(44) Which built-in method returns the calling string values conversions?
toLowerCase() method returns the calling string value converted to lower case.
toUpperCase() method returns the calling string value converted to upper case.
toString() method returns the string representation of the number's value.

No comments: