Typescript 102 // Data & Variables Flashcards
What is a variable?
Variable Definition: A variable is a user-defined storage of data
What is a variable declaration/initialisation?
A variable must be declared before using in a program. Declaration is stating the variable name with the keyword, essentially reserving the memory space. Initialisation is giving that declared variable a value.
What is good practice for declaring variables that have no value?
Initialise to 0 or Null
What are the 3 keywords used to declare variables
let, const, var
What is the initialisation syntax for a LET declaration?
let var_name = value; (e.g. let num1 = 5; )
How many times can you declare the same variable with the keyword LET?
Once only; the let declaration does not allow for redeclaration, only changing the value stored. In other words, there is no need to use let when changing values. (e.g. let num1 = 5; num1 = 6;
What is the characteristic of a variable declared with const (as opposed to let or var)?
It is constant and cannot be reassigned.
Which variable declaration keywords allow for reassigning variables to new values?
let & var.
const reassignment is invalid.
What is the declaration syntax for a CONST declaration?
const var_name = value; (e.g. const num1 = 5; )
Why is initialisation essential when declaring using const?
It is not possible to reassign the variable when using const, so the first declaration must contain the data value it is assigned to.
What is the similarity between LET and VAR variable declarations
They store the variable as a modifiable value.
What is the declaration syntax for a var declaration?
var var_name = value; (e.g. var num1 = 5; )
Why is let generally preferable to var?
Compiler errors: If something has gone wrong the compiler will show up, meaning problems can be more easily caught
Linearity: let variables cannot be used before declaration, which encourages linearity in the code
Single Declaration: Makes it less likely for accidental reassignment errors.
Typescript has the ability to add static typing to variable declarations. What is static typing?
Static typing is the ability to make a variable always of the same type, meaning if the wrong data type is used there will be a compiler error.
What is the typescript static typing initialisation Syntax?
let var_name:datatype = value;
const var_name:datatype = value;
var var_name:datatype = value;
(e.g. let num1:number = 5;)