General Flashcards
What is the difference between recursion and iteration?
Recursion is when a statement in a function calls itself repeatedly. The iteration is when a loop repeatedly executes until the controlling condition becomes false.
The primary difference between recursion and iteration is that is arecursionis a process, always applied to a function. Theiterationis applied to the set of instructions which we want to get repeatedly executed.
Key Differences Between Recursion and Iteration
- Recursion is when a method in a program repeatedly calls itself whereas, iteration is when a set of instructions in a program are repeatedly executed.
- A recursive method contains a set of instructions, statement calling itself, and a termination condition whereas iteration statements contain initialization, increment, condition, set of instruction within a loop and a control variable.
- A conditional statement decides the termination of recursion and control variable’s value decide the termination of the iteration statement.
- If the method does not lead to the termination condition it enters to infinite recursion. On the other hand, if the control variable never leads to the termination value the iteration statement iterates infinitely.
- Infinite recursion can lead to system crash whereas, infinite iteration consumes CPU cycles.
- Recursion is always applied to method whereas, iteration is applied to a set of instruction.
- Variables created during recursion are stored on stack whereas, iteration doesn’t require a stack.
- Recursion causes the overhead of repeated function calling whereas, iteration does not have a function calling overhead.
- Due to the function calling overhead execution of recursion is slower whereas, execution of iteration is faster.
- Recursion reduces the size of code whereas, iterations make a code longer.
Why add ‘use strict’?
“use strict”
The directive looks like a string: “use strict” or ‘use strict’. When it is located at the top of a script, the whole script works the “modern” way.
What can be placed before ‘use strict’?
only comments can be placed before use strict
Name the different type conversions.
String Conversion – Occurs when we output something. Can be performed with String(value). The conversion to string is usually obvious for primitive values.
Numeric Conversion – Occurs in math operations. Can be performed with Number(value).
Boolean Conversion – Occurs in logical operations. Can be performed with Boolean(value).
What is a binary operator ? whats a unary operator?
An operator is unary if it has a single operand. For example, the unary negation - reverses the sign of a number:
let x = 1;
x = -x;
alert( x ); // -1, unary negation was applied
An operator is binary if it has two operands. The same minus exists in binary form as well:
let x = 1, y = 3; alert( y - x ); // 2, binary minus subtracts values
Shorter numberic conversion
Numeric conversion, unary +.
alert( +true ); // 1
alert( +”” ); // 0
The difference between function declaration and function expressions?
Function Declaration: a function, declared as a separate statement, in the main code flow.
// Function Declaration function sum(a, b) { return a + b; } Function Expression: a function, created inside an expression or inside another syntax construct. Here, the function is created at the right side of the “assignment expression” =:
// Function Expression let sum = function(a, b) { return a + b; }; The more subtle difference is when a function is created by the JavaScript engine.
A Function Expression is created when the execution reaches it and is usable only from that moment.
Once the execution flow passes to the right side of the assignment let sum = function… – here we go, the function is created and can be used (assigned, called, etc. ) from now on.
Function Declarations are different.
A Function Declaration can be called earlier than it is defined.
Another special feature of Function Declarations is their block scope.
What are linters?
Linters are tools that can automatically check the style of your code and make improving suggestions.
• JSLint– one of the first linters.
• JSHint– more settings than JSLint.
• ESLint– probably the newest one.
What is Babel?
When we use modern features of the language, some engines may fail to support such code. Just as said, not all features are implemented everywhere.
Here Babel comes to the rescue.
Babel is a transpiler. It rewrites modern JavaScript code into the previous standard.
Actually, there are two parts in Babel:
First, the transpiler program, which rewrites the code. The developer runs it on their own computer. It rewrites the code into the older standard. And then the code is delivered to the website for users. Modern project build systems like webpack provide means to run transpiler automatically on every code change, so that it’s very easy to integrate into development process.
Second, the polyfill.
New language features may include new built-in functions and syntax constructs. The transpiler rewrites the code, transforming syntax constructs into older ones. But as for new built-in functions, we need to implement them. JavaScript is a highly dynamic language, scripts may add/modify any functions, so that they behave according to the modern standard.
A script that updates/adds new functions is called “polyfill”. It “fills in” the gap and adds missing implementations.
Two interesting polyfills are:
core js that supports a lot, allows to include only needed features.
polyfill.io service that provides a script with polyfills, depending on the features and user’s browser.
So, if we’re going to use modern language features, a transpiler and a polyfill are necessary.
What is the difference between transpiler and compiler?
A source-to-source translator, source-to-source compiler (S2S compiler), transcompiler or transpiler[1][2] is a type of translator that takes the source code of a program written in a programming language as its input and produces an equivalent source code in the same or a different programming language. A source-to-source translator converts between programming languages that operate at approximately the same level of abstraction, while a traditional compiler translates from a higher level programming language to a lower level programming language. For example, a source-to-source compiler may perform a translation of a program from Python to JavaScript, while a traditional compiler translates from a language like C to assembler or Java to bytecode
What types of Numbers do we have?
In modern JavaScript, there are two types of numbers:
Regular numbers in JavaScript are stored in 64-bit format IEEE-754, also known as “double precision floating point numbers”. These are numbers that we’re using most of the time, and we’ll talk about them in this chapter.
BigInt numbers, to represent integers of arbitrary length. They are sometimes needed, because a regular number can’t exceed 253 or be less than -253. As bigints are used in few special areas, we devote them a special chapter BigInt.
Provide details about regular numbers.
They are 64 bit, range from 2tothepowerof 53 to -2**53, 52 of them are used to store the digits, 11 of them store the position of the decimal point (they are zero for integer numbers), and 1 bit is for the sign.
for large numbers we use e or e-
1e-3 = 1 / 1000 (=0.001)
// -6 divides by 1 with 6 zeroes 1.23e-6 = 1.23 / 1000000 (=0.00000123),
We can have hex, binary and octal numbers:
alert( 0xff ); // 255 hexadecimal
let a = 0b11111111; // binary form of 255
let b = 0o377; // octal form of 255
alert( a == b ); // true, the same number 255 at both sides
There are only 3 numeral systems with such support. For other numeral systems, we should use the function parseInt
What does num.toString(base) do?
The method num.toString(base) returns a string representation of num in the numeral system with the given base.
For example:
let num = 255;
alert( num.toString(16) ); // ff
alert( num.toString(2) ); // 11111111
The base can vary from 2 to 36. By default it’s 10.
Common use cases for this are:
base=16 is used for hex colors, character encodings etc, digits can be 0..9 or A..F.
base=2 is mostly for debugging bitwise operations, digits can be 0 or 1.
base=36 is the maximum, digits can be 0..9 or A..Z. The whole latin alphabet is used to represent a number. A funny, but useful case for 36 is when we need to turn a long numeric identifier into something shorter, for example to make a short url. Can simply represent it in the numeral system with base 36:
alert( 123456..toString(36) ); // 2n9c
When do we use two dots to call a method?
Please note that two dots in 123456..toString(36) is not a typo. If we want to call a method directly on a number, like toString in the example above, then we need to place two dots .. after it.
If we placed a single dot: 123456.toString(36), then there would be an error, because JavaScript syntax implies the decimal part after the first dot. And if we place one more dot, then JavaScript knows that the decimal part is empty and now goes the method.
Also could write (123456).toString(36).