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
Person(
    '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
672
votes
17 answers

How to interpolate variables in strings in JavaScript, without concatenation?

I know in PHP we can do something like this: $hello = "foo"; $my_string = "I pity the $hello"; Output: "I pity the foo" I was wondering if this same thing is possible in JavaScript as well. Using variables inside strings without using…
DMin
  • 10,049
  • 10
  • 45
  • 65
662
votes
17 answers

Define a global variable in a JavaScript function

Is it possible to define a global variable in a JavaScript function? I want use the trailimage variable (declared in the makeObj function) in other functions. …
hamze
  • 7,061
  • 6
  • 34
  • 43
654
votes
9 answers

How to add elements to an empty array in PHP?

If I define an array in PHP such as (I don't define its size): $cart = array(); Do I simply add elements to it using the following? $cart[] = 13; $cart[] = "foo"; $cart[] = obj; Don't arrays in PHP have an add method, for example, cart.add(13)?
AquinasTub
  • 8,435
  • 6
  • 22
  • 15
639
votes
8 answers

Expansion of variables inside single quotes in a command in Bash

I want to run a command from a bash script which has single quotes and some other commands inside the single quotes and a variable. e.g. repo forall -c '....$variable' In this format, $ is escaped and the variable is not expanded. I tried the…
Rachit
  • 6,531
  • 3
  • 16
  • 9
638
votes
27 answers

How can I check if an element exists in the visible DOM?

How do you test an element for existence without the use of the getElementById method? I have set up a live demo for reference. I will also print the code on here as well: