JavaScript Language Fundamentals Flashcards

1
Q

What is JavaScript? What do we use it for?

A

JavaScript is a scripting language that allows you to implement complex features on web pages

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

The power of JavaScript is not only limited to the front-end browsers. It can also be used on the server, or both at the same time. This can be done using Node.js.

In most cases, the usage of Node.js is within the back-end web servers that exposes APIs to the front-end.

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

JavaScript supports both object-oriented programming with prototypal inheritance as well as functional and imperative programming.

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

There are 8 basic data types in JavaScript.

-number for numbers of any kind:
integer or floating-point, integers are limited by ±(253-1).

-bigint is for integer numbers of arbitrary length.

-strings. A string may have zero or more characters, there’s no separate single-character type.

-boolean for true/false.

-null for unknown values - a standalone type that has a single value null.

-undefined for unassigned values - a standalone type that has a single value undefined.

-object and symbols for more complex data structures.

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 isNaN function?

A

The isNaN() function determines whether a value is an illegal number (Not-a-Number).
This function returns true if the value equates to NaN. Otherwise it returns false.

NaN is a property of the global object. In other words, it is a variable in global scope.
The initial value of NaN is Not-A-Number — the same as the value of Number. In modern browsers, NaN is a non-configurable, non-writable pro

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

The data type of a function is object

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

What about an array?

A

The JavaScript Array class is a global object that is used in the construction of arrays; which are high-level, list-like objects. Type is object

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 used to represent an intentional absence of value. It represents a variable whose value is undefined. It accepts only one value, which is null. The Null keyword is used to define the Null type in TypeScript, but it is not useful because we can only assign a null value to it.

Undefined represents uninitialized variables in TypeScript and JavaScript. It has only one value, which is undefined.

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

A JavaScript object is an entity having state and behavior (properties and method).

There are 3 ways to create objects:
-By object literal
-By creating instance of Object directly (using new keyword)
-By using an object constructor (using new keyword)

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

The syntax of JSON was inspired by the JavaScript Object Literal notation, but there are differences between them.

JSON stands for “JavaScript Object Notation”. It’s a string representation of a JavaScript object. It always has the type string.
You can turn a JavaScript object into a string with JSON.stringify

The quotes are mandatory on JSON

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

The most common way to define a function in JavaScript is by using the function keyword, followed by a unique function name, a list of parameters (that might be empty), and a statement block surrounded by curly braces.

There are also expression functions. A function expression is very similar in syntax to a function declaration. The major difference is that a function expression does not need a function name. Function expressions are a part of another statement.

Arrow functions are an ES6 addition and are meant to be a syntactically compact alternative to function expressions. Arrow functions are defined using a pair of parentheses containing a list of parameters, followed by a fat arrow => and then the function statements with curly braces {}.

Anonymous functions are always loaded using a variable name. Anonymous, as the name suggests, allows creating a function without any names identifier. It can be used as an argument to other functions.

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

Let
Let makes use of block scope. If you declare a variable with the let keyword, it will be accessible inside of a current block scope created with a block statement.

Const
Const behaves the same as let when it comes to scopes. It is, as the name suggests, a constant.

Var
Before ES6 the only type of variable declaration keyword we had was var. Variables declared with var are function-scoped. It means, that declaring a variable with var makes it accessible throughout the whole function.

Global variables declared with the var keyword are attached to the window object (or the global object, if you are using Node.js). Even if you declare variables at the bottom of the function, they are going to be hoisted to the top (it does not happen with let and const).

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

Global Variables − A global variable has global scope which means it can be defined anywhere in your JavaScript code using the var keyword or window global object

The proper way is to use window object. And use the syntax like the following example:

var window.iAmGlobal = “some val”; //Global variable declaration with window

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

Having many global variables is always a bad thing because it’s easy to forget you declared a variable somewhere and accidentally re-declare it somewhere else. If your first variable was local then you don’t have a problem.

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

What is function and variable hoisting?

A

Variable hoisting is not the same as function hoisting- it is unique in ways of its own. Remember there are 2 ways of creating functions- Function Declaration and Function Expression.

In Javascript, function declarations hoist the function definitions. This means that these functions can be used even before they are declared.

Unlike variables, a function declaration doesn’t just hoist the function’s name. It also hoists the actual function definition.

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

What is the global object in client-side JavaScript?

A

The global object provides variables and functions that are available anywhere. By default, those that are built into the language or the environment.

17
Q

What are some built-in functions (methods on the global object)?

A

alert() Displays an alert box with a message and an OK button

close() Closes the current window

open()Opens a new browser window

prompt()Displays a dialog box that prompts the visitor for input

18
Q

How would you set some code to run at a specified time in the future? What about repeatedly?

A

JavaScript Callback function are the most special and important function of JavaScript whose main aim is to pass another function as a parameter where the callback function runs which means one function when infused into another function with the parameters is again called as per the requirement. The parameters passed by the function at the time of invocation must be kept in mind by passing otherwise it might create compilation error at the time of calling the function.

19
Q

Explain how the guard and default operators work

A

&& operators on a return value provides us a guarding clause - IE return loggedIn && username would return username only if ‘loggedin’ is true|| default operator gives us a way to say return ‘something’ and if its not here return some default value ie return username || “user not found”

20
Q

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

A

Callbacks are functions that you pass into another function to be invoked at a later time. Self invoking functions are wrapped around parentheses and another set of parentheses is adjacent to it.

Example: (function() { console.log(“Hey”)})();

21
Q

What is closure and when should you use it?

A

A closure is a function having access to the parent scope, even after the parent function has closed.The variable add is assigned to the return value of a self-invoking function. The self-invoking function only runs once. It sets the counter to zero (0), and returns a function expression. This way add becomes a function.

It can access the counter in the parent scope. This is called a JavaScript closure. It makes it possible for a function to have “private” variables. The counter is protected by the scope of the anonymous function, and can only be changed using the add function.

22
Q

Use the object literal syntax to create an object with some properties

A

let wezleysobject = { name: “wezley”, occupation: “teacher”, level: “9001”}

23
Q

What is a truthy or falsy value? List the falsy values.

A

Falsy values are evaulated to false in certain contexts. The falsy values are: 0, null, undefined, false, “”, and NaN.

Everything else is defined as truthy.

24
Q

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

A

The === operator is strict equality evauation. == will attempt to do type conversion.

25
Q

Explain the template literal syntax

A

Use backticks, ``, instead of quotation marks. Inside the backticks, type ${variable}

26
Q

What is a Promise?

A

A promise is an object that may produce a single value some time in the future: either a resolved value, or a reason that it’s not resolved

27
Q

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

A

Arrays in js are objects that have list-like properties; the elements in an array can be accessed with an integer (called an index). Since js arrays are not fixed, the size can be changed like this: arr.length = 0;

28
Q

List some array methods and explain how they work

A

.filter(),
.map(),
.join()

29
Q

Explain what “strict mode” does

A

Enables errors to be thrown when variables are set to a different type than what they were.

30
Q

Explain how inheritance works in JS

A

JS inheritance is prototype based. JS has objects instead of classes and each object has a private member that points to its ‘prototype object’. That prototype also has a prototype object and so on. Eventually a prototype will be set to null identifying the start or ‘parent’ of the prototype chain.

31
Q

What does the “this” keyword refer to?

A

The “this” keyword in JavaScript refers to the object that is currently being processed. In the context of a function, “this” refers to the object that is calling the function. In the context of an event, “this” refers to the object that triggered the event.

Alternative Answer:

The JavaScript this keyword refers to the object it belongs to.

It has different values depending on where it is used:

In a method, this refers to the owner object.

Alone, this refers to the global object.

32
Q

Explain the concept of lexical scope

A

A lexical scope means that a variable defined outside a function can be accessible inside another defined after the variable declaration. But the opposite is not true

33
Q

What are some methods on the function prototype?

A

apply(),
bind(),
toString()

toString() prints the string representation of the function.

bind() creates a new function and sets its this property to the incoming argument.

apply() calls a function and sets the this property.

34
Q

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

A

Statement will return the boolean value false.

We need to use Number.Epsilon as a “tolerance” for number comparisons between floating point numbers.