Skip to main content

Featured

What is Coroutine in Android?

Coroutines are available for the Kotlin language. Coroutines are simply cooperative functions.   Our Android applications often have multiple concurrent and interdependent functionalities. We provide instructions to the system through code, also known as a program, to perform a set of sequential instructions.  During the execution of these instructions, a specific instruction may take longer than expected, causing the next line of code to wait until the previous line of code has completed its execution. This scenario is known as blocking program execution. To avoid blocking program execution, the code must be executed on a separate thread. A thread is a single sequential flow of control. However, creating and handling multiple threads can be difficult and expensive in terms of OS operations and memory usage. To address this issue, we can use Coroutines in Kotlin. Coroutines are cooperative functions that are executed on threads as needed in a cooperative manner. They help manage long-r

Javascript Variables and Constants

Variables in JavaScript is used to store values. Variables hold the values which later on going to be used in program.

In JavaScript, you can declare variables using these 3 keywords : var, let and const. Below are the examples of how you can declare variables with var, let and const keywords.


Declare variable with var keyword :
var x = 5;

Declare variable with let keyword :
let x = 5;

Declare variable with const keyword :
const x = 5;
It looks like similar declarations for let, var and const keywords but there is a minor difference in it.

Variables you have declared with const will have a fixed value. Once declared and assigned, you cannot alter the value of the variable in later stage.


Variables declared with var keyword is having scope of throughout of program. Which means if you have declared any variable with var keyword then it will be accessible at all places in the program (will see example for the same in upcoming tutorials)


Variables declared with let keyword is having limited scope that is block scope. The variable which is declared with let keyword can only be accessible within which it is declared. Refer to the below example for better clarity.

function fun()
{
	let x = 5;
	console.log(x);
}
fun();
console.log(x);
Output :

As you can see in above example variable x is declared in a function (you can consider it a block as of now, in future we will see about functions in JavaScript). You can access that variable within the function. However, if you are trying to access it outside the function, you will get an error.

Variables declared with const keyword will have a constant value. Once a value is assigned to a variable, you cannot change its value in later part of the program.

const x = 5;
x = 10; // this will give you error

You can change the value of variables which are declared with let and var keyword after the declaration.


You must have to assign value at the time declaration of constant as you cannot assign value at later stage.


There are some rules for declaring variables in JavaScript : 

  1. The variable name must starts with alphabet or an underscore(_) or dollar sign ($). Variable names cannot start with a number.
    let x = 5; // valid
    let _x = 5; //valid
    let $x = 5; // valid
    
  2. You cannot use keywords as variable name in JavaScript.
    let var = 5; // Invalid
    let function = 5; // Invalid
    
  3. JavaScript is a case-sensitive language. This means variables with different cases will be treated as separate variables.
    let x = 5;
    let X = 5; // These both variables are different 
    

Now you have an idea about variables, so we can learn more about data types in the next post.



Comments

Popular Posts