JavaScript Interview Questions Flashcards
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.
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 complementary to and integrated with HTML.
JavaScript is open and cross-platform.
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.
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.
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.
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 };
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
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];
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] }
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 }
How many types of functions JavaScript supports?
A function in JavaScript can be either named or anonymous.
How to define a anonymous function?
An anonymous function can be defined in similar way as a normal function but it would not have any name.
Can you assign a anonymous function to a variable?
Yes! An anonymous function can be assigned to a variable.
Can you pass a anonymous function as an argument to another function?
Yes! An anonymous function can be passed as an argument to another function.
What is arguments object in JavaScript?
JavaScript variable arguments represents the arguments passed to a function.
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
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
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
What is the purpose of ‘this’ operator in JavaScript?
JavaScript famous keyword this always refers to the current context.
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.
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.
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.
What is closure?
Closures are created whenever a variable that is defined outside the current scope is accessed from within some inner scope.
Give an example of closure?
Following example shows 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
Which built-in method returns the character at the specified index?
charAt() method returns the character at the specified index.
Which built-in method combines the text of two strings and returns a new string?
concat() method returns the character at the specified index.
Which built-in method calls a function for each element in the array?
forEach() method calls a function for each element in the array.
Which built-in method returns the index within the calling String object of the first occurrence of the specified value?
indexOf() method returns the index within the calling String object of the first occurrence of the specified value, or −1 if not found.
Which built-in method returns the length of the string?
length() method returns the length of the string.
Which built-in method removes the last element from an array and returns that element?
pop() method removes the last element from an array and returns that element.
Which built-in method adds one or more elements to the end of an array and returns the new length of the array?
push() method adds one or more elements to the end of an array and returns the new length of the array.
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.
Which built-in method sorts the elements of an array?
sort() method sorts the elements of an array.
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.
Which built-in method returns the calling string value converted to lower case?
toLowerCase() method returns the calling string value converted to lower case.
Which built-in method returns the calling string value converted to upper case?
toUpperCase() method returns the calling string value converted to upper case.