JS Flashcards

1
Q

What is JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the data types supported by JavaScript?

A

The data types supported by JavaScript are:

Undefined
Null
Boolean
String
Symbol
Number
Object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the features of JavaScript?

A

Following are the features of JavaScript:

It is a lightweight, interpreted programming language.
It is designed for creating network-centric applications.
It is complementary to and integrated with Java.
It is an open and cross-platform scripting language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is JavaScript a case-sensitive language?

A

Yes, JavaScript is a case sensitive language. The language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the advantages of JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you create an object in JavaScript?

A
JavaScript supports Object concept very well. You can create an object using the object literal as follows −
var emp = {
name: "Daniel",
age: 23
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you create an Array in JavaScript?

A

You can define arrays using the array literal as follows-

var x = [];
var y = [1, 2, 3, 4, 5];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a name function in JavaScript & how to define it?

A

A named function declares a name as soon as it is defined. It can be defined using function keyword as :

function named(){
// write code here
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can you assign an anonymous function to a variable and pass it as an argument to another function?

A

Yes! An anonymous function can be assigned to a variable. It can also be passed as an argument to another function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is argument objects in JavaScript & how to get the type of arguments passed to a function?

A

JavaScript variable arguments represents the arguments that are 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(7); //==> "number", 1
func("1", "2", "3"); //==> "string", 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the scopes of a variable in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the purpose of ‘This’ operator in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is Callback?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Closure? Give an example.

A

Closures are created whenever a variable that is defined outside the current scope is accessed from within some inner scope. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Name some of the built-in methods and the values returned by them.

A

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.

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the variable naming conventions in JavaScript?

A

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.

17
Q

How does TypeOf Operator work?

A

The typeof operator is used to get the data type of its operand. The operand can be either a literal or a data structure such as a variable, a function, or an object. It 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.

18
Q

How to create a cookie using JavaScript?

A

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”;

19
Q

How to read a cookie using JavaScript?

A

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 strings’ split() function to break the string into key and values.

20
Q

How to delete a cookie using JavaScript?

A

If you want to delete a cookie so that subsequent attempts to read the cookie return nothing, you just need to set the expiration date to a time in the past. You should define the cookie path to ensure that you delete the right cookie. Some browsers will not let you delete a cookie if you don’t specify the path.

21
Q

What is the difference between Attributes and Property?

Attributes- provide more details on an element like id, type, value etc.

A

Property- is the value assigned to the property like type=”text”, value=’Name’ etc.

Q23. List out the different ways an HTML element can be accessed in a JavaScript code.

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.

22
Q

In how many ways a JavaScript code can be involved in an HTML file?

A

There are 3 different ways in which a JavaScript code can be involved in an HTML file:

Inline
Internal
External
An inline function is a JavaScript function, which is assigned to a variable created at runtime. You can differentiate between Inline Functions and Anonymous since an inline function is assigned to a variable and can be easily reused. When you need a JavaScript for a function, you can either have the script integrated in the page you are working on, or you can have it placed in a separate file that you call, when needed. This is the difference between an internal script and an external script.

23
Q

What are the ways to define a variable in JavaScript?

A

The three possible ways of defining a variable in JavaScript are:

Var – The JavaScript variables statement is used to declare a variable and, optionally, we can initialize the value of that variable. Example: var a =10; Variable declarations are processed before the execution of the code.

Const – The idea of const functions is not allow them to modify the object on which they are called. When a function is declared as const, it can be called on any type of object.

Let – It is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it’s defined in.

24
Q

What is a Typed language?

A

Typed Language is in which the values are associated with values and not with variables. It is of two types:

Dynamically: in this, the variable can hold multiple types; like in JS a variable can take number, chars.

Statically: in this, the variable can hold only one type, like in Java a variable declared of string can take only set of characters and nothing else.

25
Q

What is the difference between Local storage & Session storage?

A

Local Storage – The data is not sent back to the server for every HTTP request (HTML, images, JavaScript, CSS, etc) – reducing the amount of traffic between client and server. It will stay until it is manually cleared through settings or program.

Session Storage – It is similar to local storage; the only difference is while data stored in local storage has no expiration time, data stored in session storage gets cleared when the page session ends. Session Storage will leave when the browser is closed.

26
Q

What is the difference between the operators ‘==‘ & ‘===‘?

A

The main difference between “==” and “===” operator is that formerly compares variable by making type correction e.g. if you compare a number with a string with numeric literal, == allows that, but === doesn’t allow that, because it not only checks the value but also type of two variable, if two variables are not of the same type “===” return false, while “==” return true.

27
Q

What is the difference between null & undefined?

A

Undefined means a variable has been declared but has not yet been assigned a value. On the other hand, null is an assignment value. It can be assigned to a variable as a representation of no value. Also, undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.

28
Q

What is the difference between undeclared & undefined?

A

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.

29
Q

Name some of the JavaScript Frameworks

A

A JavaScript framework is an application framework written in JavaScript. It differs from a JavaScript library in its control flow. There are many JavaScript Frameworks available but some of the most commonly used frameworks are:

Angular
React
Vue

30
Q

What is the difference between window & document in JavaScript?

A

JavaScript window is a global object which holds variables, functions, history, location.

The document also comes under the window and can be considered as the property of the window.