Basic JavaScript

0

Varibles are the containers for storeing data values.

In JavaScript, a variable can be declared using var, let, const keywords.

JavaScript identifiers are case-sensitive.

You can assign a value to a variable using the = operator when you declare it or after the declaration and before accessing it.

  • var keyword is used to declare variables since JavaScript was created. It is confusing and error-prone when using variables declared using var.
  • let keyword removes the confusion and error of var. It is the new and recommended way of declaring variables in JavaScript.
  • const keyword is used to declare a constant variable that cannot be changed once assigned a value.

There are two types of variables in JavaScript : local variable and global variable.

There are some rules while declaring a JavaScript variable (also known as identifiers).

  • Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.
  • After first letter we can use digits (0 to 9), for example value1.
  • JavaScript variables are case sensitive, for example x and X are different variables.
  • Variable names are case-sensitive in JavaScript. So, the variable names msg, MSG, Msg, mSg are considered separate variables.
  • Variable names can contain letters, digits, or the symbols $ and _.
  • A variable name cannot start with a digit 0-9.
  • A variable name cannot be a reserved keyword in JavaScript, e.g. var, function, return cannot be variable names.

Global Variables

Variables declared out of any function are called global variables. They can be accessed anywhere in the JavaScript code, even inside any function.

Local Variables

Variables declared inside the function are called local variables of that function. They can only be accessed in the function where they are declared but not outside.

The following example includes global and local variables.

Example: Global and Local Variable
let greet = "Hello " // global variable

function myfunction(){
    let msg = "JavaScript!"; 
    alert(greet + msg); //can access global and local variable
}

myfunction();

alert(greet);//can access global variable
alert(msg); //error: can't access local variable

Learn global and local scope in JavaScript for more information.

Declare Variables without var and let Keywords

Variables can be declared and initialized without the var or let keywords. However, a value must be assigned to a variable declared without the var keyword.

The variables declared without the var keyword become global variables, irrespective of where they are declared. Visit Variable Scope in JavaScript to learn about it.

The variables declared without the var keyword become global variables, irrespective of where they are declared. Visit Variable Scope in JavaScript to learn about it.

It is Recommended to declare variable using the let keyword.

Example: Variable Declaration Without var or let Keyword
function myfunction(){
	msg = "Hello JavaScript!"; 
}

myfunction();
alert(msg); // msg becomes global variable so can be accessed here
Points to Remember
  1. Variables can be defined using let keyword. Variables defined without let or var keyword become global variables.
  2. Variables should be initialized before accessing it. Unassigned variable has value undefined.
  3. JavaScript is a loosely-typed language, so a variable can store any type value.
  4. Variables can have local or global scope. Local variables cannot be accessed out of the function where they are declared, whereas the global variables can be accessed from anywhere.

Post a Comment

0Comments
Post a Comment (0)