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