The scope of var, let, and const
Var
The scope is global when a var variable is declared outside a function. This means that
any variable that is declared with var outside a function block is available for use in the whole
window.
var is function scoped when it is declared within a function. This means that it is available and can be
accessed only within that function.
Let
let is block scoped. A block is a chunk of code bounded by {}. A block lives in curly braces. Anything within curly braces is a block. So a variable declared in a block with let is only available for use within that block.
Const
const is also block scoped. Like let declarations, const declarations can only be accessed within the block they were declared.
The use cases of null and undefined
Null
The null keyword in JavaScript is a falsy data type that denotes that the data type is null. Falsy type refers to the coercion of null to false during conditional statement executions. Null is an object in JavaScript and represents primitive data types. A null value in JavaScript is used for referring absence of any object value and if any function or variable returns null, then we can infer that the object could not be created. If we pass null as the default parameter to any function type it would take the ‘null’ as a value passed to it.
Use case of null
- Initializing Variables: We can initialize a variable with null when we want to indicate that it doesn't currently have a meaningful value but will be assigned a value later.
- JSON Serialization: When converting JavaScript objects to JSON using JSON.stringify(), properties with null values are preserved in the resulting JSON string.
- Error Handling: In error handling, null can be used to represent the absence of an error when a function or operation returns a result. If no error occurred, you might return null to indicate success.
- Checking for Existence: We can use null to check if a variable or object property has been assigned a value or if it's still undefined.
- Placeholder in Data: In some cases, null is used as a placeholder value in data structures or databases to represent missing or unknown information.
Undefined
undefined is a property of the global object. That is, it is a variable in global scope. In all non-legacy browsers, undefined is a non-configurable, non-writable property. Even when this is not the case, avoid overriding it. A variable that has not been assigned a value is of type undefined. A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned.
Use case of undefined
- Implicit Absence: When a variable is declared but not assigned a value, it is automatically initialized to undefined. This is often the initial state of variables.
- Function Return Values: If a function doesn't explicitly return a value, JavaScript returns undefined by default.
- Missing Object Properties: When you attempt to access an object property that doesn't exist, JavaScript returns undefined.
- Function Arguments: If a function parameter is not provided with an argument, it is assigned the value undefined.
- Check for Variable Existence: You can use typeof or conditional statements to check if a variable exists or is undefined. This can be useful for error handling.
What is a REST API?
Representational State Transfer (REST) is an architectural style that defines a set of constraints to be used for creating web services. REST API is a way of accessing web services in a simple and flexible way without having any processing.
REST technology is generally preferred to the more robust Simple Object Access Protocol (SOAP) technology because REST uses less bandwidth, simple and flexible making it more suitable for internet usage. It’s used to fetch or give some information from a web service. All communication done via REST API uses only HTTP request.