Vocab/Methods definitions Flashcards
What are the primitive data types that cannot be mutated? So in other words, what are the primitive types?
Primitive types are String, Number, Boolean, null, undefined and symbol
What data types CAN be mutated?
Composite Types: Objects, Functions
Example: Adding element to an array.
What does Lexical scope mean?
Lexical (or local) scope means that in a nested group of functions, the inner functions have access to the variables and other resources of their parent scope.
Lexical scope means that in a nested group of functions, the inner functions have access to the variables and other resources of their parent scope.
This means that the child’s functions are lexically bound to the execution context of their parents.
Lexical scope is sometimes also referred to as static scope.
function grandfather() { var name = 'Hammad'; // 'likes' is not accessible here function parent() { // 'name' is accessible here // 'likes' is not accessible here function child() { // Innermost level of the scope chain // 'name' is also accessible here var likes = 'Coding'; } } }
What does the term Block Scope mean?
A block scope is the area within if, switch conditions or for and while loops. Generally speaking, whenever you see {curly brackets}, it is a block. In ES6, const and let keywords allow developers to declare variables in the block scope, which means those variables exist only within the corresponding block.
function foo(){ if(true){ var fruit1 = 'apple'; //exist in function scope const fruit2 = 'banana'; //exist in block scope let fruit3 = 'strawberry'; //exist in block scope
} console.log(fruit1); console.log(fruit2); console.log(fruit3); }
foo(); //result: //apple //error: fruit2 is not defined //error: fruit3 is not defined
what is .charAt( ) method and what does it do?
he String object’s charAt( ) method returns a new string consisting of the single UTF-16 code unit located at the specified offset into the string. For example:
const sentence = ‘The quick brown fox jumps over the lazy dog.’;
const index = 4;
console.log(`The character at index ${index} is ${sentence.charAt(index)}`); // expected output: "The character at index 4 is q"
An integer between 0 and str.length - 1. If the index cannot be converted to the integer or no index is provided, the default is 0, so the first character of str is returned.
what is a forEach( ) method? and what is it used for?
The forEach() method calls a function for each element in an array.
const fruits = ["apple", "orange", "cherry"]; fruits.forEach(myFunction);
another example: const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
// expected output: "a" // expected output: "b" // expected output: "c"
What is an arrow function? =>
An arrow function expression is a compact alternative to a traditional function expression, but is limited and can’t be used in all situations.
// Traditional Anonymous Function function (a){ return a + 100; }
// Arrow Function Break Down
// 1. Remove the word "function" and place arrow between the argument and opening body bracket (a) => { return a + 100; }
// 2. Remove the body braces and word "return" -- the return is implied. (a) => a + 100;
// 3. Remove the argument parentheses a => a + 100;
what is the (…) ? called and what is it used for?
Its called the spread snytax. The spread syntax is commonly used to make shallow copies of JS objects. Using this operator makes the code concise and enhances its readability.
For Example:
let array1 = [‘h’, ‘e’, ‘l’, ‘l’, ‘o’];
let array2 = […array1];
console.log(array2); [‘h’, ‘e’, ‘l’, ‘l’, ‘o’] // output
What is Math.floor( ) method? what is it used for?
The Math.floor( ) function returns the largest integer less than or equal to a given number.
Syntax: Math.floor(x)
For example:
Math.floor( 45.95); // 45 Math.floor( 45.05); // 45 Math.floor( 4 ); // 4 Math.floor(-45.05); // -46 Math.floor(-45.95); // -46
What is Math.ceil( ) method used for? give an example.
The Math.ceil( ) function always rounds a number up to the next largest integer.
Syntax: Math.ceil(x)
Example:
console.log(Math.ceil(.95));
// expected output: 1
console.log(Math.ceil(4)); // expected output: 4
console.log(Math.ceil(7.004)); // expected output: 8
console.log(Math.ceil(-7.004)); // expected output: -7
What is a set method? and what is it used for?
The Set object lets you store unique values of any type, whether primitive values or object references.
let myArray = [‘value1’, ‘value2’, ‘value3’]
// Use the regular Set constructor to transform an Array into a Set let mySet = new Set(myArray)
mySet.has(‘value1’) // returns true
// Use the spread operator to transform a set into an Array. console.log([...mySet]) // Will show you exactly the same Array as myArray
What is a new method? what is it used for? Give an example
The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.
Syntax: new constructor[ ( [arguments] ) ]
For Example:
function Car(make, model, year) { this.make = make; this.model = model; this.year = year; }
const car1 = new Car(‘Eagle’, ‘Talon TSi’, 1993);
console.log(car1.make); // expected output: "Eagle"
What is a reduce( ) method used for? Give me an example.
The reduce( ) method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value. Perhaps the easiest-to-understand case for reduce() is to return the sum of all the elements in an array:
What is a Callback Function?
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
This technique allows a function to call another function
A callback function can run after another function has finished
For Example:
function greeting(name) { alert('Hello ' + name); }
function processUserInput(callback) { var name = prompt('Please enter your name.'); callback(name); }
processUserInput(greeting);
What is a Dom Tree?
A DOM tree is a kind of tree whose nodes represent an HTML or XML document’s contents. Each HTML or XML document has a unique DOM tree representation. For example, the following document