JavaScript Flashcards

1
Q

What is JavaScript? What do we use it for?

A

JavaScript is a text-based programming language used both on the client-side and server-side that allows you to make web pages interactive.

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

Can we run JavaScript in a web browser, on a server, or both?

A

JavaScript can execute not only in the browser, but also on the server, or actually on any device that has a special program called the JavaScript engine

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

What programming paradigm(s) does JS support?

A

It is a prototype-based, multi-paradigm scripting language that is dynamic, and supports object-oriented, imperative, and functional programming styles.

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

What are the data types in JS?

A

Primitive Types:
Boolean - true/false
Null - nonexistent or invalid object address
Undefined - a variable that has not been assigned a value
Number - integers between -2^53 - 1 and 2^53 -1 Includes NaN - Not a Number
BigInt - whole numbers larger than 2^53 -1
String - text data
Symbol - a unique and immutable value.
Non-Primitive:
Objects - a value in memory which is referenced by an identifier

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

What is the type of NaN? What is the is NaN function?

A
Not a Number is a number.
The isNaN() method returns true if a value is NaN.
The isNaN() method converts the value to a number before testing it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the data type of a function?

A

In JavaScript, functions are objects. But because of an error in JS, functions return a function

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

What is the data type of an array?

A

Arrays are just regular objects

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

What is the difference between undefined and null?

A

null is an assigned value. It means nothing. undefined means a variable has been declared but not defined yet

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

What are JS objects? what is the syntax?

A
In JavaScript, an object is a standalone entity, with properties and type.
const student = {
    firstName: 'ram',
    class: 10
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is JSON? Is it different from JS objects?

A

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax.

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

What are some ways you can use functions in JS?

A

Function Declaration - declares a function and runs it
Function Expression - puts a function inside an expression
Generator Function - A function that can stop midway and continue from there

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

What are the different scopes of variables in JS?

A

JavaScript variables have only two scopes. Global Variables − A global variable has global scope which means it can be defined anywhere 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
13
Q

What are the different ways to declare global variables?

A

You can define a global variable with var, let, or const and they are defined as global as long as they are declared outside of any function

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

Is it a best practice to use global variables? Why or why not?

A

It is not good practice to use global variables.
Global variables can be altered by any part of the code, making it difficult to remember or reason about every possible use.

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

What are callback functions? What about self-invoking functions?

A

A callback function is placed in the parameters of a function and can be used to run another function as soon as the original function is complete
A self-invoking expression is invoked (started) automatically, without being called. Add parenthesis around the function followed by parenthesis

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

What is the difference between == and ===? Which one allows for type coercion?

A

== - Used for comparing two variables but ignores the datatype

=== - Is used for comparing two variables and also checks that their datatypes are equal as well

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

What is a Promise?

A

A promise in JS is an asynchronous function that allows you to define the handlers for success and for failure of a function.

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

What are arrays in JS? Can you change their size?

A

Array is a single variable that is used to store different elements. It is often used when we want to store a list of elements and access them by a single variable.JavaScript allows you to change the value of the array length property.

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

List some array methods and explain how they work

A

toString() converts an array to a string of (comma separated) array values.
join() method also joins all array elements into a string.
pop() method removes the last element from an array
push() method adds a new element to an array (at the end)

20
Q

Explain what “strict mode” does

A

The purpose of “use strict” is to indicate that the code should be executed in “strict mode”.
With strict mode, you can not, for example, use undeclared variables.
All modern browsers support “use strict” except Internet Explorer 9 and lower

21
Q

Explain how inheritance works in JS

A

When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype

22
Q

What does the “this” keyword refer to?

A

The this keyword refers to the current object in a method or constructor.

23
Q

What are some methods on the function prototype?

A

hasOwnProperty()-Returns a boolean indicating whether an object contains the specified property as a direct property of that object and not inherited through the prototype chain.
isPrototypeOf()-Returns a boolean indication whether the specified object is in the prototype chain of the object this method is called upon.
propertyIsEnumerable()-Returns a boolean that indicates whether the specified property is enumerable or not.
toLocaleString()-Returns string in local format.
toString()-Returns string.
valueOf-Returns the primitive value of the specified object.

24
Q

What will happen when I try to run this code: console.log(0.1+0.2==0.3) ?

A

It will give out false.

25
Q

What new features did ES6 introduce?

A
ES6 includes the following new features:
arrows
classes
Destructuring
default + rest + spread
let + const
iterators + for..of
unicode
modules
module loaders
promises
math + number + string + array + object APIs
26
Q

What is the difference between var, let, and const keywords?

A

Var -It can be accessed without initialization as its default value is “undefined”. Functional scope. Can be updated and reused
Let- It cannot be accessed without initialization, as it returns an error. Block scope. Can be updated but reused
Const- It cannot be accessed without initialization, as it cannot be declared without initialization. Block scope. Can’t be updated or reused. Must be declared with initialization

27
Q

Give the syntax for template literals / string interpolation?

A

string text

string text ${expression} string text

28
Q

What’s the difference between a normal function declaration and an arrow function?

A

Regular functions are both constructible and callable, arrow functions are only callable
Regular Function: Dynamic use of ‘this’ keyword
simple invocation - this = global object
method invocation - this = object owning the method
indirect invocation - this = the value of the first argument
constructor invocation - this = newly created function instance
arrow function - this = the this value from outside the function

29
Q

Does JS have classes? If so, when were they introduced?

A

There are no classes in the class-based OOP sense of the word. They are used as a template for creating objects as to help implement OOP concepts.

30
Q

What is object and array destructuring? What is the rest/spread operator?

A

Destructuring is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

The rest operator (…) takes multiple elements and condenses them into a single object. Can be used to take a varying amount of parameters.

the spread operator (…[]) allows us to spread the values of an array into the parameters of a function

31
Q

How would you set default values for parameters to a function?

A
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
function multiply(a, b = 1 ){ }
32
Q

What is the difference between for-of and for-in loops?

A
for in loops over enumerable property names of an object.
for of (new in ES6) iterates over values of an iterable object
33
Q

What’s the benefit of computed property names?

A

That allows you to put an expression in brackets [] , that will be computed and used as the property name.

34
Q

Explain the async/await keywords. Why is it preferred to use this instead of .then() methods?

A

The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains. Async functions may also be defined as expressions.
It is just a wrapper to restyle code and make promises easier to read and use.

35
Q

What built-in data structures does JavaScript provide?

A

JavaScript has built in support for the following data structures: Array. Set. Map.

36
Q

What is the DOM? How is it represented as a data structure? What object in a browser environment allows us to interact with the DOM?

A

The Document Object Model (DOM) is the data representation of the objects that comprise the structure and content of a document on the web.
The DOM represents a document with a logical tree. Each branch of the tree ends in a node, and each node contains objects.
The document and window objects are the objects whose interfaces you generally use most often in DOM programming.

37
Q

List some ways of querying the DOM for elements

A
getElementsByTagName()
getElementsByClassName()
getElementById()
querySelector()
querySelectorAll()
38
Q

How would you insert a new element into the DOM?

A

To add a new element to the HTML DOM, you must create the element (element node) first, and then append it to an existing element.

39
Q

What are event listeners? What are some events we can listen for? What are some different ways of setting event listeners?

A
An event listener is a procedure in JavaScript that waits for an event to occur.
The user clicking or moving the mouse, pressing a key on the keyboard, disk I/O, network activity, or an internal timer or interrupt.
The addEventListener() method attaches an event handler to the specified element.
 What are some methods on the event object and what do they do? 
The addEventListener() method attaches an event handler to an element without overwriting existing event handlers.
40
Q

How would you submit a form using JS?

A

Create a function for JavaScript that will get executed when the link is clicked. When we click on the link, the function submitForm() will get executed.

41
Q

What is AJAX? Why do we use it?

A

AJAX stands for Asynchronous JavaScript And XML. In a nutshell, it is the use of the XMLHttpRequest object to communicate with servers. It can send and receive information in various formats, including JSON, XML, HTML, and text files.

42
Q

What are the benefits of using AJAX?

A

Read data from a web server - after a web page has loaded
Update a web page without reloading the page
Send data to a web server - in the background

43
Q

Are there any downsides of using AJAX?

A

Open-source
Search Engines cannot index Ajax pages can not be indexed by Google as well as other search engines.
The usage of Ajax can cause difficulties for your web pages to debug as well as make them prone to possible security issues in the future.

44
Q

Explain why it is important that AJAX is asynchronous

A

It’s asynchronous in that it doesn’t lock up the browser. If you fire an Ajax request, the user can still work while the request is waiting for a response. When the server returns the response, a callback runs to handle it. -1 Asynchronous means “not at the same time”.

45
Q

List the steps to sending an AJAX request

A

Update a web page without reloading the page.
Request data from a server - after the page has loaded.
Receive data from a server - after the page has loaded.
Send data to a server - in the background.

46
Q

List the different ready states of the XmlHttpRequest object

A

The XMLHttpRequest object can be in five states: UNSENT, OPEN, SENT, LOADING and DONE.

47
Q

How does the fetch API differ from the XHR object?

A

fetch() allows you to make network requests similar to XMLHttpRequest (XHR). The main difference is that the Fetch API uses Promises, which enables a simpler and cleaner API, avoiding callback hell and having to remember the complex API of XMLHttpRequest.