JavaScript Flashcards
What are Template Literals?
Template literals provide an easy way to interpolate variables and expressions into strings.
The method is called string interpolation.
The syntax is as follows:
${…}
What are variables?
Variables are containers for storing data values.
How many types of variables do you know in JavaScript?
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.
What are the data types in JavaScript?
*Primitive Data Types:
boolean - QA use
number - QA use
string - QA use
undefined
bigint
Symbol
null
* Non-Primitive Data Types Object Array
What is the difference between == and ===?
== checks the value
=== checks data type and the value:
let a = ‘1’
let b = 1
console.log(a===b) // False
What is switch statement?
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;
}
}
What is Conditional (Ternary) operator?
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”.
What are loops and how many types of loops are there in JavaScript?
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
What are branching statements?
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
What are arrays?
- 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 []
ARRAY MANIPULATION
toString() method
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 do you access the last element in an array?
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 do you add elements to the array?
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
pop()
removes the last element
push()
adds an element to the end of an array
shift()
removes the first element from the array
unshift()
adds an element to the beginning
splice()
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' ]
indexOf()
returns the first index at which a given element can be found in the array. “-1” if the item is not found
includes()
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
What is a multidimensional array?
Multidimensional array in JavaScript is known as array inside another array.
What is a function?
Functions let you group a series of statements together to perform a specific task. It is a piece of reusable code.
What is a global variable?
Variable that is created outside of the function
What is a local variable?
Variable that is created inside the function
What is hoisting in JavaScript?
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.
What is a callback function?
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)
What are closures?
When your inner function has access to variables and parameters
For more info https://www.w3schools.com/js/js_function_closures.asp
What are arrow functions?
Arrow functions allow us to write shorter function
syntax:
let myFunction = (a, b) => a * b;
What is a map?
- 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’]
forEach() loop
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.
Spread Operator
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.
What is a Map Object?
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.
What is a Set Object?
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 }