Seamlessly integrate your JavaScript code with your favorite APIs and databases, using WayScript.

Write JavaScript code.

See Code Editors for more information on composing code in WayScript.

Working with Variables

📥 Inputs

WayScript's JS editor lets you reference Variables that exist in your program. To do this, you can reference them through the variables dictionary.

As an example, let's create a variable called "Location" and set a value of "Brooklyn, NY"

You can reference the Location variable in your JavaScript with this code:

location = variables[ "Location" ];
// Or, as a JS object
location = variables.Location;

More abstractly, you reference any variable with the format:

const my_var = variables[ "<var_name>" ];

📤 Outputs

Return Data from Your JavaScript

You can also output data from your JS code and turn these into variables that can be used by other Modules in your program.

You can do this with the variables dictionary in the following format:

variables[ "<Var Name>" ] = variable;
// example
const currentTime = new Date();
variables[ "Date" ] = currentTime.toISOString();

🖨 Output Raw JavaScript Code

You can output the raw JavaScript code from the code editor by enabling the "JavaScript Code" output. This is useful when building a website using WayScript and inserting JavaScript into HTML.

Raw JS Code Output as a Variable

If you are building JS for a webpage, you might only want to output the code as text and not run it at all. This can be achieved by disabling the "Run Code" setting.

Running JavaScript

While working on your code, you can press the "Run Code" button inside the module to run the JavaScript and see updated results.

📦 Adding Dependencies

If you would like to use an npm module in your code, you can declare those dependencies in the "Requirements.txt" file.

Declare these dependencies on separate lines, using the format you would for an npm-install command.

You can then consume these packages in your code:

const wayscript = require( 'wayscript' );
wayscript.apiKey = 'YOUR_API_KEY';
let programId = 1234;
wayscript.runProgram( programId );

Importing Code from Other Files

You can import code from the other JavaScript files in your File Browser. For example, suppose you have a file called foo.js, which has an exported function bar.

You can import and use the bar method in scratch.js by using a relative import:

const foo = require('./foo');
Example of importing and using the function "bar" from "foo.js"