The scope of var, let, and const in JavaScript
JavaScript is a dynamically typed language which means that we can simply use `var`, `let`, and `const` directly to assign variables without indicating their types. However, `var`, `let`, and `const` each have different scopes and characteristics.
- var: `var` is function scoped, which means that they can be accessed anywhere inside a function after they are declared, even in another function which maybe called inside that function. `var` is also global scoped, which means that after declaration, they can be accessed anywhere throughout the whole script, and even re-declared. This might cause problems in a large script where a developer might need to declare a lot of variables inside or outside functions and do a lot of mathematical calculations.
- let: Both `let` and `const` are block scoped, which means that they can only be accessed within the block of code they are declared in. If any variables/parameters are declared inside a specific region (e.g. inside curly brackets `{}` of a function) they cannot be called outside that region. If they are declared at the very beginning of the script, at the outermost block, they can be accessed throughout the block. Moreover, `let` and `const` cannot be re-declared so they are more useful for larger scripts with a lot of functions and variables. But `let` differs from `const` in one crucial way, that it can be re-assigned. This means that variables declared with `let` can be edited according to need, and hence, they are perfect for numerical calculations such as keeping the count of a variable inside a loop and changing it every time.
- const: `const` variables have a lot of similar behaviours to `let`, like they are also block scoped, and they cannot also be re-declared. However, `const` does not allow re-assign of the values of the variables, which means that the values stay constant, which is also very useful in large code spaces and declaring functions. But, arrays or objects declared using `const` still retain some of their functionalities like 'push', 'pop', or changing object property values.
Some use cases of null and undefined
-
undefined:
In JavaScript, an `undefined` variable is a variable which hasn't been initialized. For e.g. if we define
let name;and don't assign a value to it, it is set as `undefined`. In the same way, if we define a function but do not return anything from it, that function is also `undefined`. For e.g.const handleSum = () => {2+2;}
Morover, if we call a function which takes some parameters as input, but do not pass it any arguments, the values of those function parameters are set as `undefined`. For e.g.const sum = (a,b) => a+b;
sum(); // the values of a and b in the function call are set as undefined
Similarly, an empty return of a function, calling a non-existent property of an object, an index call of an array outside it's index-range, deleting an array item, all these actions lead to `undefined` values. A variable can also directly be declared as undefined. In JavaScript,typeof undefinedis `undefined`. -
null:
`null` is defined as the absence of value of a variable. It is a primitive JavaScript datatype like `undefined`, and is used in many cases to set the initial value of a variable to null or nothing, so that it can be changed later accordingly. In JS,
typeof nullis object. Both `null` and `undefined` are falsey values which return false in case of a condiiton.
Understanding REST API
REST API methods are a series of C.R.U.D.(Create, Read, Update, Delete) operations which can be performed on an API resource database, with the help of a server. There are a handful of very useful REST API methods which are briefly discussed below.
- GET: Used to get information (read data) about an API resource.
- POST: An API create operation, used to create an API resource.
- PUT: An API update operation that checks the database for existing data, then edits the data with new resources if there is any change, or creates new data if it was previously non-existent.
- PATCH: API update operation, used to change or modify an API resource.
- DELETE: Used to delete an API resource entirely.