JavaScript Flashcards

1
Q

What are Template Literals?

A

Template literals provide an easy way to interpolate variables and expressions into strings.

The method is called string interpolation.

The syntax is as follows:

${…}

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

What are variables?

A

Variables are containers for storing data values.

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

How many types of variables do you know in JavaScript?

A

There are 3 types of variables:
1. var - in QA we will not use “var”, because var is mostly used by developers. 1) var creates duplicate containers; 2. there is invisible var. var can be reached outside the block scope, i.e. has global access
2. let - when we use “let”, the value is mutable, i.e. you can reassign the value
3. const - we cannot reassign or redeclare the value; exists in a block scope. When using const, you need to assign the value once const is used, it is immutable.

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

What are the data types in JavaScript?

A

*Primitive Data Types:
boolean - QA use
number - QA use
string - QA use
undefined
bigint
Symbol
null

* Non-Primitive Data Types
	Object
	Array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between == and ===?

A

== checks the value

=== checks data type and the value:
let a = ‘1’
let b = 1
console.log(a===b) // False

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

What is switch statement?

A

The switch statement is used to perform different actions based on different conditions.

Use the switch statement to select one of many code blocks to be executed. This is how it works:
1. The switch expression is evaluated once.
2. The value of the expression is compared with the values of each case.
3. If there is a match, the associated block of code is executed.
4. If there is no match, the default code block is executed.

                                                     Example: let weather = "hot"

switch(weather){
case “hot”:{
console.log(“Go to the beach”)
break;
}
case “cold”: {
console.log(“Stay at home”)
break;
}
case “snow”:{
console.log(“Build a snowman”)
break;
}
default:{
console.log(“Invalid input”)
break;
}
}

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

What is Conditional (Ternary) operator?

A

It is a conditional operator that assigns a value to a variable based on some condition.
Syntax
variablename = (condition) ? value1:value2

                                                Example let voteable = (age < 18) ? "Too young":"Old enough";

If the variable age is a value below 18, the value of the variable voteable will be “Too young”, otherwise the value of voteable will be “Old enough”.

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

What are loops and how many types of loops are there in JavaScript?

A

Loops are handy, if you want to run the same code over and over again, each time with a different value.

JavaScript supports different kinds of loops:

for –> loops through a block of code a number of times
for/in –> loops through the properties of an object
for/of –> loops through the values of an iterable object
while –> loops through a block of code while a specified condition
is true
do/while –> also loops through a block of code while a specified
condition is true

The most commonly used ones are For, While, and Do While

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

What are branching statements?

A

Branching statements allow us to add conditions in our Code.
There are mainly four kinds of branching statements available in JavaScript:

              1. If Statement
              2. If else Statement
              3. Else if Statement
              4. Switch Statement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are arrays?

A
  • Array is a non primitive data type
  • An array can hold many values under a single name, and you can access the values by referring to an index number.
  • Each array has an index starting from 0 and we access an array element by referring to the index number:
    const cars = [“Saab”, “Volvo”, “BMW”];
    let car = cars[0];
  • to access each element of an array we use brackets []
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

ARRAY MANIPULATION

toString() method

A

toString() –> converts an array to a string of (comma separated) array values.

	const fruits = ["Banana", "Orange", "Apple", "Mango"];
	console.log(fruits.toString());// Banana,Orange,Apple,Mango
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you access the last element in an array?

A

The last element in an array can be accessed by subtracting 1 from from the length of an array:

const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
let fruit = fruits[fruits.length - 1];

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

How do you add elements to the array?

A

The easiest way to add a new element to an array is using the push() method:

	const fruits = ["Banana", "Orange", "Apple"];
	fruits.push("Lemon");  // Adds a new element (Lemon) to the end of fruits array 

New element can also be added to an array using the length property:

	const fruits = ["Banana", "Orange", "Apple"];
	fruits[fruits.length] = "Lemon";  // Adds "Lemon" to fruits
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

pop()

A

removes the last element

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

push()

A

adds an element to the end of an array

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

shift()

A

removes the first element from the array

17
Q

unshift()

A

adds an element to the beginning

18
Q

splice()

A

It adds new items to an array. You can delete elements in an array by passing TWO arguments. Starting index, number of elements to be deleted.

let cars = [“Toyota”, “BMW”, “Tesla”, “Ford”, “Buick”, “Rivian”]
cars.splice(0,3)
// [ ‘Ford’, ‘Buick’, ‘Rivian’ ]
console.log(cars)

with 3 arguments–>

				let cars = ["Toyota", "BMW", "Tesla", "Ford", "Buick", "Rivian"]
				cars.splice(1,2, "Subaru")// in index 1 ("BMW"), remove 2 elements ("BMW" and "Tesla"), and add "Subaru"
				console.log(cars)//[ 'Toyota', 'Subaru', 'Ford', 'Buick', 'Rivian' ]
19
Q

indexOf()

A

returns the first index at which a given element can be found in the array. “-1” if the item is not found

20
Q

includes()

A

determines whether an array includes a certain value among its entries, returning true or false

let animals = [“ant”, “bison”, “camel”, “duck”, “elephant”]
console.log(animals.includes(“ant”)) // true

21
Q

What is a multidimensional array?

A

Multidimensional array in JavaScript is known as array inside another array.

22
Q

What is a function?

A

Functions let you group a series of statements together to perform a specific task. It is a piece of reusable code.

23
Q

What is a global variable?

A

Variable that is created outside of the function

24
Q

What is a local variable?

A

Variable that is created inside the function

25
Q

What is hoisting in JavaScript?

A

Hoisting is JavaScript’s default behavior of moving all declarations to the top of the current scope. In relation to functions, it is calling a function even before creating it.

26
Q

What is a callback function?

A

A callback is a function passed as an argument to another function. This technique allows a function to call another function. A callback function can run after another function has finished.
SYNTAX:

function dosomething(x) {
let first_name = “Mitko”
x(first_name)
}

function test(result) {

console.log("My name is " + " " + result) }

dosomething(test)

27
Q

What are closures?

A

When your inner function has access to variables and parameters

For more info https://www.w3schools.com/js/js_function_closures.asp

28
Q

What are arrow functions?

A

Arrow functions allow us to write shorter function

syntax:

let myFunction = (a, b) => a * b;

29
Q

What is a map?

A
  • map() creates a new array from calling a function for every array element.
  • map() does not execute the function for empty elements.
  • map() does not change the original array.

let a = [“a”, “b”, “c”, “d”, “f”]

let test = a.map((x, y) => { // x = first argument is an element, y = second argument is an index of the element
return y
})
console.log(test)

Array.from() method
let a = “Javascript”
let b = Array.from(a)// [ ‘J’, ‘a’, ‘v’, ‘a’, ‘s’, ‘c’, ‘r’, ‘i’, ‘p’, ‘t’]

30
Q

forEach() loop

A

forEach loop works only with arrays

forEach () method and map() method work very similarly. However, map () method returns an array from the array, while
forEach() method is just a loop which iterates throgh an array. That is why we should assign map() method to a variable.

31
Q

Spread Operator

A

The spread operator (…) expands an iterable (like an array) into more elements.

This allows us to quickly copy all or parts of an existing array into another array.

32
Q

What is a Map Object?

A

Map object can be created by calling new map object. It has a key and value. The key must be in a string format.

let test = new Map()
let account = “account”

The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.

33
Q

What is a Set Object?

A

Set object is similar to the Map object, however the Set object stores only unique values

let mySet = new Set()
my.set.add(1)
my.set.add(“a”)
my.set.add(false)
console.log(mySet)// { 1, ‘a’, false }