Questions tagged [variables]

THIS IS AMBIGUOUS; USE SPECIFIC-LANGUAGE TAGS WHENEVER APPLICABLE. A variable is a named data storage location in memory. Using variables, a computer program can store numbers, text, binary data, or a combination of any of these data types. They can be passed around in the program.

A variable is a named data storage location in memory. Using variables a computer program can store numbers, textual data, , or a combination of any of these data types. The data can be passed around in a program by copying it from variable to variable or by referencing variables, i.e. defining from which variable a receiving code part is to take the contained data.

Variables which are only accessible within a certain function are termed "local variables". A "global variable", or one with indefinite scope, may be referred to anywhere in the program.

In some , variables are constrained by a specific data type. Data types may vary across languages, but share many commonalities.

Primitive data types usually include:

  • character, char, string, varchar (text)
  • byte, short, int, tinyint, integer, long (whole numbers)
  • double, decimal, float (real numbers)
  • bit, boolean (true/false)
  • date, datetime (date and time values)
  • object (any value, including composite types)
  • binary, raw, varbinary (that store stream of system data in binary form)

Composite data types consist of usually more than one of the primitive types and/or even other composite types.

# an example composite type, in pseudo code
    'First name'  : string,
    'Surname'     : string,
    'Birthday'    : date,
    'CanProgram'  : boolean

Some languages contain extra primitives: Tuples (Python), Linked Lists (Lisp), Hash Tables (Lisp, Perl, Python, Lua, D).

Some programming languages allow variables that store functions, that can be stored in data structures, passed as parameters to other functions, or returned as a result from other functions.

Memory allocation

The specifics of variable allocation and the representation of their values vary widely, both among programming languages and among implementations of a given language. Many language implementations allocate space for local variables (whose extent lasts for a single function call) on the stack, and their memory is automatically reclaimed when the function returns. More generally, in name binding, the name of a variable is bound to the address of some particular block (contiguous sequence) of bytes in memory, and operations on the variable manipulate that block. Referencing is more common for variables whose values have large or unknown sizes when the code is compiled. Such variables reference the location of the value instead of storing the value itself, which is allocated from a pool of memory called the heap.

More information and reference material on Wikipedia.

53839 questions
86 answers

How do JavaScript closures work?

How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves? I have seen the Scheme example given on Wikipedia,…
Bite code
  • 578,959
  • 113
  • 301
  • 329
34 answers

How can I determine if a variable is 'undefined' or 'null'?

How do I determine if a variable is undefined or null? My code is as follows: var EmpName = $("div#esd-names div#name").attr('class'); if(EmpName == 'undefined'){ // DO SOMETHING };
But when I…
  • 39,964
  • 34
  • 108
  • 180
31 answers

JavaScript check if variable exists (is defined/initialized)

Which method of checking if a variable has been initialized is better/correct? (Assuming the variable could hold anything (string, int, object, function, etc.)) if (elem) { // or !elem or if (typeof elem !== 'undefined') { or if (elem != null) {
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
38 answers

How to check if a variable is set in Bash

How do I know if a variable is set in Bash? For example, how do I check if the user gave the first parameter to a function? function a { # if $1 is set ? }
  • 182,215
  • 215
  • 566
  • 871
27 answers

What is the scope of variables in JavaScript?

What is the scope of variables in javascript? Do they have the same scope inside as opposed to outside a function? Or does it even matter? Also, where are the variables stored if they are defined globally?
  • 57,436
  • 10
  • 26
  • 20
26 answers

How do I check if a variable is an array in JavaScript?

How do I check if a variable is an array in JavaScript? if (variable.constructor == Array)
Andy McCluggage
  • 37,618
  • 18
  • 59
  • 69
29 answers

"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP

I'm running a PHP script and continue to receive errors like: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php on line 10 Notice: Undefined index: my_index C:\wamp\www\mypath\index.php on line 11 Warning: Undefined array…
  • 442,112
  • 142
  • 972
  • 1,088
15 answers

How do I check if a variable exists?

I want to check if a variable exists. Now I'm doing something like this: try: myVar except NameError: # Do something. Are there other ways without exceptions?
Max Frai
  • 61,946
  • 78
  • 197
  • 306
52 answers

How to trim whitespace from a Bash variable?

I have a shell script with this code: var=`hg st -R "$path"` if [ -n "$var" ]; then echo $var fi But the conditional code always executes, because hg st always prints at least one newline character. Is there a simple way to strip whitespace…
too much php
  • 88,666
  • 34
  • 128
  • 138
50 answers

Set environment variables from file of key/value pairs

TL;DR: How do I export a set of key/value pairs from a text file into the shell environment? For the record, below is the original version of the question, with examples. I'm writing a script in bash which parses files with 3 variables in a certain…
  • 10,942
  • 3
  • 17
  • 13
15 answers

How to check a not-defined variable in JavaScript

I wanted to check whether the variable is defined or not. For example, the following throws a not-defined error alert( x ); How can I catch this error?
  • 11,187
  • 5
  • 24
  • 25
22 answers

How to check if type of a variable is string?

Is there a way to check if the type of a variable in python is a string, like: isinstance(x,int); for integer values?
  • 10,173
  • 3
  • 15
  • 6
43 answers

Static variables in JavaScript

How can I create static variables in Javascript?
  • 32,970
  • 17
  • 67
  • 87
5 answers

What is the purpose of the single underscore "_" variable in Python?

What is the meaning of _ after for in this code? if tbh.bag: n = 0 for _ in tbh.bag.atom_set(): n += 1
  • 28,057
  • 62
  • 134
  • 188
7 answers

Capturing multiple line output into a Bash variable

I've got a script 'myscript' that outputs the following: abc def ghi in another script, I call: declare RESULT=$(./myscript) and $RESULT gets the value abc def ghi Is there a way to store the result either with the newlines, or with '\n'…
  • 7,949
  • 5
  • 26
  • 21
2 3
99 100