Assignment Expression

Summary

Assign a value to a variable.

Syntax

variable = expression

Parameters

variable
The variable, property, or array element to be given a value.
expression
Any legal expression.

Description

The = operator computes the value of the expression on the right, and then assigns it to the variable, property, or array element on the left. The expression on the right is evaluated prior to assignment:

1
2
int x = 2;
x = 3 * x + 5; // x is assigned the value 11

Using the assignment operator, it is also possible to initialize variables later:

1
2
int x;
x = 5;

Examples

Basic Usage
1
2
3
4
5
6
import System;
 
int x = 1;
Console.log(x); // 1
x = 10;
Console.log(x); // 10
Delayed Variable Initialization
1
2
3
4
5
6
7
8
9
int x;  // Variable not initialized
 
bool maybeTrue = false;
if (maybeTrue) {
    x = 10;
}
else {
    x = 50;
}

See Also

Share

HTML | BBCode | Direct Link