JavaScript Variables, Functions, and Arguments in Minecraft

We’re going to learn three basic JavaScript concepts in this article: Variables, Functions, and Arguments.


A variable is a bucket that you wrote a name on. Imagine you have three buckets: A, B, and C.

three buckets: A, B, and C

You can put all sorts of things in variables, just like you can put all sorts of things in buckets. Imagine that you’ve got a sandwich in A, a hammer in B, and a bottle of soda in C.

If you tell your friend to give you A, they’ll give you a sandwich.
If you tell your friend to give you A + C, they’ll give you a sandwich and a soda.
If you ask for A + B, you’ll get a sandwich and a hammer.

Let’s try this in Minecraft.

var a = 'sandwich'

Type: /js var a = "sandwich"; and hit Enter.

What happened? Nothing, right? Well, nothing you could see. You just told the computer to make a bucket, name it a, and put the word “sandwich” in it. You didn’t tell it to say anything.

echo a

Let’s tell it to say what’s in a. Type: /js echo(a); and hit Enter.

output of echo a

It said “sandwich”.

We could talk about a bunch of rules for variables, but let’s just go with the big three:

  1. Variable names should be letters or even words, but no spaces or punctuation.
  2. You should use “var ” in front of a variable the first time you use it in a program. It tells the computer to create a new bucket with that name.
  3. Don’t put quotes around variable names or numbers, only around words or sentences you want to put inside variables.

For example:
2 + 2 = 4
"2" + "2" = "22"

When you put quotes around it, the computer thinks it’s a word, and "word" + "word" in JavaScript is "wordword". JavaScript doesn’t add spaces between words you add together. It just puts them right next to each other.

You might get this wrong a few times before you remember it. Don’t worry and don’t get mad at yourself. Even programmers who have been programming for 20 years make mistakes. When you figure out what your mistake was and fix it, that’s called “debugging,” and it’s an important skill in programming. So if you make a mistake, that’s just a chance to learn debugging.

Functions and Arguments

When you told the computer to echo(a), you were asking it to perform the echo function. There are functions that are built into JavaScript and much of JavaScript programming involves creating your own functions. For now, we’re going to talk about how to use functions other people have created. We’ll get into creating your own in a later topic.

After echo there was (a). That was called an “argument.” An argument is simply a value you send to a function, when you’re asking it to run, and you put it inside parentheses after the function’s name. It can either be a variable, or the value itself. echo("sandwich") would have the same result as echo(a) in the variable example above.

One of my favorite functions in ScriptCraft is the box(). That requires four arguments, separated with commas. Try typing: /js box(5,2,3,2);

Creating a block of oak planks

Every block in Minecraft has a number associated with it. This function makes a solid square or rectangle out of the material identified by the first number. The next three numbers are the width, height, and length. So /js box(5,2,3,2); tells Minecraft to make a rectangle out of blocks of oak planks (or block type 5) that is 2 blocks wide, 2 blocks deep, and 3 blocks high.

The oak planks as a result of box(5,2,3,2)

How good is your multiplication? What if, instead of being oak planks, that was TNT? What if it was 5 x 5 x 5 blocks in size? With one function, you can create a brick of TNT with 125 TNT blocks in it. Pretty cool.

We’ll learn more about functions and arguments when we explore the drone functions in the next blog post.

Go back to the main Minecraft modding page.

Leave a Reply

Your email address will not be published. Required fields are marked *