What's a block definition?
Stay organized with collections
Save and categorize content based on your preferences.
A block definition is an object that defines a custom block. For example, it
defines the block's look and feel, (text, fields, connections, colour, etc.), as
well as its behavior (block-level event handler, etc.).
For example, this block:

can be defined in JSON or JavaScript as follows:
JSON
Blockly.common.defineBlocksWithJsonArray([{
"type": "string_length",
"message0": 'length of %1',
"args0": [
{
"type": "input_value",
"name": "VALUE",
"check": "String"
}
],
"output": "Number",
"colour": 160,
"tooltip": "Returns number of letters in the provided text.",
"helpUrl": "http://www.w3schools.com/jsref/jsref_length_string.asp"
}]);
defineBlocksWithJsonArray
creates a block definition from a JSON object.
JavaScript
Blockly.Blocks['string_length'] = {
init: function() {
this.appendValueInput('VALUE')
.setCheck('String')
.appendField('length of');
this.setOutput(true, 'Number');
this.setColour(160);
this.setTooltip('Returns number of letters in the provided text.');
this.setHelpUrl('http://www.w3schools.com/jsref/jsref_length_string.asp');
}
};
When you use JavaScript, you create the block definition directly.
How block definitions work
A block definition is a mixin containing
function-valued properties. When a new block is instantiated, these properties
are copied to the newly created block object. Blockly calls these functions to
invoke custom behavior.
Because the definition functions are mixed in to the block object:
- The
this
keyword in definition functions refers to the block object. That
is, it can be used to access the public methods and properties in the
Block
(or
BlockSvg
) class.
Blockly defines a small number of functions you can use to customize blocks. The
most common of these is init
, which Blockly calls to initialize a block and
which is used to define the block's look and feel. For a complete list, see the
function-valued properties in the
Block
and
BlockSvg
classes.
These properties effectively form an interface for block definitions to
implement; all of them are optional.
Block definitions can also have custom functions, although Blockly will not call
these directly. Instead, they can be used to implement other functions. For
example, a block-level event handler (the Block.onchange
property) could
delegate different events to different custom functions.
How to create block definitions
There are several different ways to create block definitions:
- Use the Blockly Developer Tools to define the look and feel of your block. Copy
the generated JSON or JavaScript to your code and add custom code as needed.
For more information, see Blockly Developer Tools.
- Find a similar block, copy its definition, and modify it as needed. For more
information, see Modify existing
definitions.
- Write a block definition by hand. For more information, see JSON and
JavaScript.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-04-14 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-04-14 UTC."],[],[],null,["A **block definition** is an object that defines a custom block. For example, it\ndefines the block's look and feel, (text, fields, connections, colour, etc.), as\nwell as its behavior (block-level event handler, etc.).\n\nFor example, this block:\n\ncan be defined in JSON or JavaScript as follows: \n\nJSON \n\n Blockly.common.defineBlocksWithJsonArray([{\n \"type\": \"string_length\",\n \"message0\": 'length of %1',\n \"args0\": [\n {\n \"type\": \"input_value\",\n \"name\": \"VALUE\",\n \"check\": \"String\"\n }\n ],\n \"output\": \"Number\",\n \"colour\": 160,\n \"tooltip\": \"Returns number of letters in the provided text.\",\n \"helpUrl\": \"http://www.w3schools.com/jsref/jsref_length_string.asp\"\n }]);\n\n`defineBlocksWithJsonArray` creates a block definition from a JSON object.\n\nJavaScript \n\n Blockly.Blocks['string_length'] = {\n init: function() {\n this.appendValueInput('VALUE')\n .setCheck('String')\n .appendField('length of');\n this.setOutput(true, 'Number');\n this.setColour(160);\n this.setTooltip('Returns number of letters in the provided text.');\n this.setHelpUrl('http://www.w3schools.com/jsref/jsref_length_string.asp');\n }\n };\n\nWhen you use JavaScript, you create the block definition directly.\n\nHow block definitions work\n\nA block definition is a [mixin](https://en.wikipedia.org/wiki/Mixin) containing\nfunction-valued properties. When a new block is instantiated, these properties\nare copied to the newly created block object. Blockly calls these functions to\ninvoke custom behavior.\n\nBecause the definition functions are mixed in to the block object:\n\n- The `this` keyword in definition functions refers to the block object. That is, it can be used to access the public methods and properties in the [`Block`](/blockly/reference/js/blockly.block_class) (or [`BlockSvg`](/blockly/reference/js/blockly.blocksvg_class)) class.\n\nBlockly defines a small number of functions you can use to customize blocks. The\nmost common of these is `init`, which Blockly calls to initialize a block and\nwhich is used to define the block's look and feel. For a complete list, see the\nfunction-valued properties in the\n[`Block`](/blockly/reference/js/blockly.block_class#properties) and\n[`BlockSvg`](/blockly/reference/js/blockly.blocksvg_class#properties) classes.\nThese properties effectively form an interface for block definitions to\nimplement; all of them are optional.\n\nBlock definitions can also have custom functions, although Blockly will not call\nthese directly. Instead, they can be used to implement other functions. For\nexample, a block-level event handler (the `Block.onchange` property) could\ndelegate different events to different custom functions.\n\nHow to create block definitions\n\nThere are several different ways to create block definitions:\n\n- Use the Blockly Developer Tools to define the look and feel of your block. Copy the generated JSON or JavaScript to your code and add custom code as needed. For more information, see [Blockly Developer Tools](/blockly/guides/create-custom-blocks/blockly-developer-tools).\n- Find a similar block, copy its definition, and modify it as needed. For more information, see [Modify existing\n definitions](/blockly/guides/create-custom-blocks/define/modify-definitions).\n- Write a block definition by hand. For more information, see [JSON and\n JavaScript](/blockly/guides/create-custom-blocks/define/json-and-js)."]]