JAVA SCRIPT TERMINOLOGIES
CREATING VARIABLES IN JAVA SCRIPT
In JavaScript, we use var, let, and const to create variables. These keywords might seem similar at first, but they control how and where your variables work in your code. Let’s explore each one and how they differ from each other.
VAR
The var is the oldest keyword to declare a variable in JavaScript. It has the Global scoped or function scoped. This means:
If you create a variable outside of a function, you can use it anywhere in your code.
If you create a variable inside a function, you can only use it within that function.
Example of var
The below code explains the use of var keyword to declare variable in java script
CODE
var a = 10
function f() {
var b = 20
console.log(a, b)
}
f();
console.log(a);
RESULT
10 20
10
Example 2 : The below example explains the behavior of var variables when declared inside a function and accessed outside of it.
CODE
Function f() {
// It can be accessible any
// where within this function
var a = 10;
console.log(a)
}
f();
// A cannot be accessible
// outside of function
console.log(a);
RESULT
10
Reference Error: a is not defined
LET
The let keyword is an improved version of the var keyword. These variables has the block scope. It can’t be accessible outside the particular code block ({block}).
The variable is only available inside the block {} where you define it.
Example 1 : The below code declares the variable using the let keyword.
CODE
let a = 10;
function f() {
let b = 9
console.log(b);
console.log(a);
}
f();
RESULT
9
10
Example 2 : The below code explains the block scope of the variables declared using the let keyword.
CODE
let a = 10;
function f() {
if (true) {
let b = 9
// It prints 9
console.log(b);
}
// It gives error as it
// defined in if block
console.log(b);
Get Kanu chukwuemeka’s stories in your inbox
Join Medium for free to get updates from this writer.
}
f()
// It prints 10
console.log(a)
RESULT
9
Reference Error: b is not defined
CONST
The const keyword has all the properties that are the same as the let keyword, except the user cannot update it and have to assign it with a value at the time of declaration. These variables also have the block scope. It is mainly used to create constant variables whose values can not be changed once they are initialized with a value.
const is similar to let, but with one big difference: once you assign a value to a const variable, you can’t change it later. Like let, const is block-scoped.
Example 1: This code tries to change the value of the const variable.
CODE
const a = 10;
function f() {
a = 9
console.log(a)
}
f();
RESULT
Type Error: Assignment to constant variable.
WHY TO USE VAR
1. It can be updated and re-declared in the same scope.
2. It can be declared without initialization.
3. The scope of a var variable is functional or global scope.
4. Variables are accessible within the entire function where they’re declared.
WHY TO USE LET
1. It can be updated and re-declared in the same scope.
2. The scope of a let variable is block scope.
3. Variables are not initialized until the code execution reaches the declaration so no hoisting issues
NULL AND UNDEFINED DATATYPES
What is undefined?
undefined is a primitive value automatically assigned to variables in certain situations:
Uninitialized Variables: A variable that is declared but not initialized will have the value undefined.
Missing Function Return: If a function does not explicitly return a value, it returns undefined by default.
Non-Existent Object Properties or Array Elements: Accessing an object property or an array element that does not exist results in undefined.
What is Null?
null is a special value in JavaScript that represents the deliberate absence of any object value. It is often used to:
Indicate “No Value”: null explicitly shows that a variable or object property should have no value.
Reset or Clear Variables: Use null to reset a variable or remove its reference to an object.
Similarities between null and undefined JavaScript.
1. Primitive data type in JavaScript.
2. Indicating Absence: Both are used to indicate that a variable does not have a meaningful value. They can often be used interchangeably in some contexts.
Differences between null and undefined in JavaScript.
1. Meaning:
- undefined typically means that a variable has been declared but has not yet been assigned a value.
- null is an assignment value that represents the intentional absence of any object value.
2. Use Cases:
- Use undefined to check for uninitialized variables.
- Use null when you want to intentionally indicate that a variable should be empty.
3. Equality:
- When using loose equality (==), null and undefined are considered equal.
CODE
console.log(null == undefined); // true
- However, with strict equality (===), they are not equal.
CODE
console.log(null === undefined); // false
DIFFERENCE BETWEEN PARAMETERS AND ARGUMENTS IN JAVASCRIPT
Parameters refer to the variables listed in a function’s declaration, defining the input that the function can accept. Arguments, however, are the actual values passed to the function when it is called, filling the parameters during execution.
1. Parameters are used in the function declaration While Arguments are used in the function invocation (call).
2. Quantity: You can define a function with multiple parameters, but you may pass a different number of arguments when calling the function.
3. Default Values: You can provide default values for parameters, which will be used if no corresponding argument is provided during the function call.





