What are Variables in Programming [C/PHP/Python/JS]
In programming languages we can store data in one of two ways: Either it already stored [Pre-Defined Data], or the user provides the data to store [use-Defined Data].
Both methods of data storage are used in all the programming Languages.
Each variable has a Unique name, a value, and a data type that are unique to it. Variable's value will vary over time, it can be Changed and Overwrite in Time to Time.
Definition: What are variables in programming
In order to store data [in programming Languages], memory locations are assigned names called variables. Depending on how the variables are assigned values, this data may be Defined or Undefined.
How to Declare a Variable in Programming Languages
Declaring a variable is another name for the process of Creating a Variable. The process for declaring a variable relies on the Programming language.
variables in any programming language should be named in such a way that The Name Itself Clarifies Its Purpose and also don't break any rules like
Variable can end with Number but Can't start with Number.
You can't use Special Characters with Variable names like "Comma, Exclamation Mark, asterisk and Quotes"
Need a Personal Tutor?
I am here to help you with your Programming journey. HTML, CSS, JavaScript, ReactJS, NextJS, C, C++, C#, SQL and more.
Variable Syntax
index.html
data_type variable_name = value;
data_type : What kind of Data we are storing [Like Integer, String, Character, boolean ETC].
Variable_name: Identification for your Variable so by using its name we can Assign, Overwrite or Access Stored Data.
value: the Data we want to store [like int a = 2, where int is my Datatype, a is my Variables name and 2 is my value that I am storing inside a].
Where storing a value in a variable called "Assignment or Value Assignment".
Declaring Variable in different Programming languages
Declaring Variables in C Language
//Syntax
Datatype variable_name = value;
index.html
//Code: Variables in C
int a = 1;
//printing Variable value in C
printf("%d", a);
Declaring Variables in JavaScript
//Syntax
type variable_name = value;
index.html
//Code: Variables in JavaScript
var a=1;
//printing Variable value in JavaScript
document.write(a);
Need a Personal Tutor?
I am here to help you with your Programming journey. HTML, CSS, JavaScript, ReactJS, NextJS, C, C++, C#, SQL and more.
Declaring Variables in Java
//Syntax
DataType variable_name = value;
index.html
//Code: Variables in Java
String a= "John";
//printing Variable value in Java
System.out.println(a);
Declaring Variables in Python
//Syntax
variable_name = value;
index.html
//Code: Variables in Python
a= "John";
//printing Variable value in Python
print(a);
Declaring Variables in PHP
//Syntax
$variable_name = value;
index.html
//Code: Variables in PHP
$a= "John";
//printing Variable value in PHP
echo $a;
Types of Variables in Different Programming Languages
Most programming language supports a total of 5 different variable types. Like:
Local Variables
Global Variables
Static/Class Variables
Constant Variable
Instance Variables
Need a Personal Tutor?
I am here to help you with your Programming journey. HTML, CSS, JavaScript, ReactJS, NextJS, C, C++, C#, SQL and more.
Local variables
Local variables is a programming language construct, present or declared locally inside the method body. As we know that function is a group of statements created with a specific purpose in mind. So, if we declare a Variable inside a Function, then that Variable only will be accessible/available inside that function. You can't access that local variable outside that Function or Method.
index.html
//JavaScript Code
function myFun(){
var a=5;
//a is accesible Here
}
//Variable a is not Accesssble here
Global Variables
A global variable is a programming language construct, present or Declared outside any function and is accessible to all functions throughout the program. A global variable is usually declared on top of all functions, as all functions can manipulate them during the program’s run time, because its available globally.
index.html
//JavaScript Code
var a=5;
function myFun(){
//a is accesible Here
}
//Variable a is ALSO Accessible here
Static/Class Variables
Static variables, also referred to as class variables, are those that are declared in a class but not inside of a function,
function Object() { [native code] }
Where this Static/Class Variable will be accessible only within that class.
Constant Variable
A variable that stays constant during the course of an experiment, even while other variables may change, is a constant variable, also known as a controlled variable. Where you can assign value once but can't overwrite the value after that.
index.html
//JavaScript Code
const a=5;
a=6; //cannot Overwrite
Need a Personal Tutor?
I am here to help you with your Programming journey. HTML, CSS, JavaScript, ReactJS, NextJS, C, C++, C#, SQL and more.
Instance Variables
A class variable is a member of the class itself, but an instance variable belongs to an instance of the class.
While there is only one copy of each static (or class) variable linked to the class itself, each instance of a class will have its own copy of an instance variable.
On This Page
Need a Personal Tutor?
I am here to help you with your Programming journey. HTML, CSS, JavaScript, ReactJS, NextJS, C, C++, C#, SQL and more.