getVar
Category: Variable Management
Namespace: web.variables.getVar
Description
Retrieves a persistent variable value by name.
Syntax
const value = await web.variables.getVar('username');
Parameters
- varName (required) - Variable name (key) to retrieve — positional argument
Returns
Promise<string|null> - Returns a promise that resolves with the variable value as a string, or null if not found
Examples
Simple example
const theme = await web.variables.getVar('theme');
if (theme) {
console.log('Theme:', theme);
} else {
console.log('Theme not set');
}
Load user preferences
// Load preferences with defaults
const language = await web.variables.getVar('language') || 'en';
const fontSize = await web.variables.getVar('fontSize') || '12';
const autoSave = await web.variables.getVar('autoSave') || 'false';
console.log('Language:', language);
console.log('Font Size:', fontSize);
console.log('Auto Save:', autoSave === 'true');
Retrieve JSON data
// Retrieve and parse JSON data
const userDataStr = await web.variables.getVar('currentUser');
if (userDataStr) {
const userData = JSON.parse(userDataStr);
console.log('User:', userData.name);
console.log('Email:', userData.email);
} else {
console.log('No user data found');
}
Use Cases
- Load user preferences on startup
- Retrieve cached data
- Check if user has completed setup
- Load last used settings
- Restore application state
Notes
- Returns
nullif variable doesn't exist - All values are returned as strings - parse as needed
- Variable names are case-sensitive
- Use
loadVariables()first to load from disk if needed - Returns the value from in-memory dictionary