Fundamentals Flashcards
What 3 keywords are used to declare variables?
let, var & const
What is the main difference between let/const and var?
let is block-scoped (only exists within the lifetime of a scope) & cannot be redeclared. Var is global scoped, and can be redeclared. Let is preferable to var.
What is the difference between let & const?
let declares a variable, const declares a constant. The value associated with a constant cannot be changed - it is immutable.
Why should const be the default keyword?
Using const offers a guarantee that is a variable should not be changed, it won’t be, which makes code more secure. Let then becomes used deliberately for variables that you plan to alter.
What are the basic/scalar types?
number string boolean undefined object (rarely used)
What are the two simple types of data collections?
object
array (/ array-like)
What does the term ‘execute’ mean?
To execute means to activate the action defined by the code.
What is a function?
A function is a scope of code to be executed when it is invoked.
What format is number data-type stored as?
64-bit floating point numbers. There are no explicit scalar integer types in TS/JS.
How would you declare a constant variable “foo” with a value of 5?
const foo = 5; // implicit const foo: number = 5; // explicit
What is the difference between implicit and explicit typing?
The type of the variable if implicit is inferred from the value assigned upon initialisation, if explicit is the specified type.
What is a literal?
Any value that is not stored in a variable (e.g. 5, “hello”, true).
Define a variable “bar” that can be a string or a number, and initialise with value 5.
let foo: string | number = 5;
What does Truthy & Falsy mean?
Truthy values are values of any type that, in any operation that requires a boolean value, evaluate as True. Falsy values in the same situation evaluate to false.
What are the standard Falsy values?
false, 0, “”, undefined, null, NaN
If foo is a number variable of 5, what would foo.toFixed(2) produce?
toFixed returns a string version of a number to fixed decimal places.
foo.toFixed(2) === “5.00”;
If foo is a number variable of 555, what would foo.toPrecision(2) produce?
toPrecision defines the precision (rounding) of a number.
foo.toPrecision(2) === “560”;
what does the toString() method do?
Returns a string version of the variable.
If foo is a number variable of 5, what would foo.toString(2) produce?
toString returns a string version of the number in this case, with the number defining the base (default base 10). In this case it would be “101”;
What is a method?
A method is a function bound to an object/class.
What are the prototypes associated with each scalar type?
Number, String, Boolean, Object, Array
If foo is a number variable of 5, what would Number.isInteger(foo) produce?
Number.isInteger() is a static method on the Number wrapper class that evaluates if a number is an integer. Number.isInteger(foo) === true;
How are string literals represented?
With single or double quotes.
I recommend using double quotes for strings, and single quotes for chars, as this is standard for other programming languages.
What are escape characters?
characters in a string that are “escaped” with a backslash () that do something to that string representation in the browser. e.g. \n will create a newline.
What are template literals?
Template Literals are represented by backticks and evaluate values in ${} to string values within the literal.
e.g. Hello ${person.name}
What String methods can be used to return a string variable to lower and upper case?
foo. toUpperCase();
foo. toLowerCase();
What property defines the length of a string?
foo.length;