Listly by edureka.co
If you are planning to start your career in JavaScript and you wish to know the skills related to it, now is the right time to dive in, when the technology is in its blossoming state. JavaScript Interview Questions will provide you with in-depth knowledge and help you prepare for your interviews.
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.
The data types supported by JavaScript are:
Undefined
Null
Boolean
String
Symbol
Number
Object
JavaScript supports Object concept very well. You can create an object using the object literal as follows −
var emp = {
name: "Daniel",
age: 23
};
You can define arrays using the array literal as follows-
var x = [];
var y = [1, 2, 3, 4, 5];
The following rules are to be followed while naming variables in JavaScript:
You should not use any of the JavaScript reserved keyword as variable name. For example, break or boolean variable names are not valid.
JavaScript variable names should not start with a numeral (0-9). They must begin with a letter or the underscore character. For example, 123name is an invalid variable name but _123name or name123 is a valid one.
JavaScript variable names are case sensitive. For example, Test and test are two different variables.
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";
Here are the list of ways an HTML element can be accessed in a Javascript code:
(i) getElementById(‘idname’): Gets an element by its ID name
(ii) getElementsByClass(‘classname’): Gets all the elements that have the given classname.
(iii) getElementsByTagName(‘tagname’): Gets all the elements that have the given tag name.
(iv) querySelector(): This function takes css style selector and returns the first selected element.
Event bubbling is a way of event propagation in the HTML DOM API, when an event occurs in an element inside another element, and both elements have registered a handle for that event. With bubbling, the event is first captured and handled by the innermost element and then propagated to outer elements. The execution starts from that event and goes to its parent element. Then the execution passes to its parent element and so on till the body element.
The parseInt() function is used to convert numbers between different bases. It takes the string to be converted as its first parameter, and the second parameter is the base of the given string.
For example-
parseInt("4F", 16)
Imports and exports help us to write modular JavaScript code. Using Imports and exports we can split our code into multiple files. For example-
//------ lib.js ------
export const sqrt = Math.sqrt;
export function square(x) {
return x * x;
}
export function diag(x, y) {
return sqrt(square(x) + square(y));
}
//------ main.js ------
{ square, diag } from 'lib';
console.log(square(5)); // 25
console.log(diag(4, 3)); // 5
The call() method calls a function with a given this value and arguments provided individually.
Syntax-
fun.call(thisArg[, arg1[, arg2[, ...]]])
The apply() method calls a function with a given this value, and arguments provided as an array.
Syntax-
fun.apply(thisArg, [argsArray])
There are a number of methods you can use to empty an array:
Method 1 –
arrayList = []
Above code will set the variable arrayList to a new empty array. This is recommended if you don’t have references to the original array arrayList anywhere else, because it will actually create a new, empty array. You should be careful with this method of emptying the array, because if you have referenced this array from another variable, then the original reference array will remain unchanged.
Method 2 –
arrayList.length = 0;
The code above will clear the existing array by setting its length to 0. This way of emptying the array also updates all the reference variables that point to the original array. Therefore, this method is useful when you want to update all reference variables pointing to arrayList.
Method 3 –
arrayList.splice(0, arrayList.length);
The implementation above will also work perfectly. This way of emptying the array will also update all the references to the original array.
Method 4 –
while(arrayList.length)
{
arrayList.pop();
}
The implementation above can also empty arrays, but it is usually not recommended to use this method often.
This is an increasingly common practice, employed by many popular JavaScript libraries. This technique creates a closure around the entire contents of the file which, perhaps most importantly, creates a private namespace and thereby helps avoid potential name clashes between different JavaScript modules and libraries.
Another feature of this technique is to allow for an easy alias for a global variable. This is often used in jQuery plugins.
JavaScript escape characters enable you to write special characters without breaking your application. Escape characters (Backslash) is used when working with special characters like single quotes, double quotes, apostrophes and ampersands. Place backslash before the characters to make it display.
For example-
document.write "I am a "good" boy"
document.write "I am a "good" boy"
Strict mode is a way to introduce better error-checking into your code.
To keep reading more questions, click here.