Skip to content

Variables

wraith4081 edited this page Aug 16, 2023 · 1 revision

Variables in WraithScript

In WraithScript, variables are used to store and manipulate data. They act as named containers that hold values of various types, such as numbers, strings, and objects. Understanding how to declare, assign, and use variables is fundamental to programming in WraithScript.

Table of Contents

Variable Declaration

To declare a variable in WraithScript, you use the var keyword followed by the variable name. The syntax is as follows:

const variableName = 123;
// OR
let variableName = 312;

Variable Assignment

Once a variable is declared, you can assign a value to it using the assignment operator =. The value can be of any compatible data type:

variableName = value;

Variable Naming

Variable names in WraithScript must adhere to the following rules:

  • They can only contain letters for now.
  • They are case-sensitive (e.g., myVariable and myvariable are treated as different variables).
  • They cannot be a reserved keyword (e.g., if, while, for).

Data Types

WraithScript supports various data types for variables, including:

  • number: Represents numeric values.
  • string: Represents sequences of characters.
  • boolean: Represents true or false values.
  • object: Represents complex data structures.
  • null: Represents the absence of a value.

Scope

Variables in WraithScript have either global or local scope:

  • Global variables are declared outside of any function and can be accessed from anywhere in the code.
  • Local variables are declared within a function and can only be accessed within that function.

Constants

To create constants (variables with values that cannot be changed), you can use the const keyword:

const PI = 3.14159;

Constants must be assigned a value when declared and cannot be reassigned later in the code.

Example

let userName = "John";
let age = 25;
let isStudent = true;

This example declares and uses variables to store a user's name, age, and student status. It then defines a function to greet the user using the stored variables.

Remember, understanding how to work with variables is crucial for effective programming in WraithScript. They allow you to manage and manipulate data, making your code more dynamic and adaptable.