Block comments
Stay organized with collections
Save and categorize content based on your preferences.
Block comments allow users to add comments to a block. Like comments in a
text-based programming language, they do not affect a block's behavior. They are
added to generated code (including code generated by custom blocks) by the
standard language code generators.

Users enter block comments with the comment icon's text editor.
By default, the comment icon is not displayed. There are two ways to display it:
- Call
Block.setCommentText
with a non-null string.
- Let the user display it by clicking "Add Comment" in the block's context
menu.
To remove the comment icon:
- Call
Block.setCommentText
with a null
.
- Let the user remove it by clicking "Remove Comment" in the block's context
menu.
Note that "Add Comment" and "Remove Comment" are displayed in the context menu
only if:
- The block is editable.
- The block is not collapsed.
- The
comments
configuration option is set to true
. If
you do not explicitly set this option, it defaults to true
if the toolbox
has categories, false
otherwise.
To get a block comment:
// Returns comment text or null if there is no comment.
myBlock.getCommentText();
To set a block comment:
// Sets comment text and displays comment icon.
myBlock.setCommentText('My comment');
To delete a block comment:
// Removes comment text and removes comment icon.
myBlock.setCommentText(null);
The ability to work with comments programmatically is not affected by the state
of the block (such as whether it is editable or collapsed) or the setting of the
comments
configuration option.
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,["# Block comments allow users to add comments to a block. Like comments in a\ntext-based programming language, they do not affect a block's behavior. They are\nadded to generated code (including code generated by custom blocks) by the\nstandard language code generators.\n\nThe comment icon\n----------------\n\nUsers enter block comments with the comment icon's text editor.\n\nBy default, the comment icon is not displayed. There are two ways to display it:\n\n- Call `Block.setCommentText` with a non-null string.\n- Let the user display it by clicking \"Add Comment\" in the block's context menu.\n\nTo remove the comment icon:\n\n- Call `Block.setCommentText` with a `null`.\n- Let the user remove it by clicking \"Remove Comment\" in the block's context menu.\n\nNote that \"Add Comment\" and \"Remove Comment\" are displayed in the context menu\nonly if:\n\n- The block is [editable](/blockly/guides/create-custom-blocks/define/block-state#editable_state).\n- The block is not collapsed.\n- The [`comments` configuration option](/blockly/guides/configure/web/configuration_struct#the_options_dictionary) is set to `true`. If you do not explicitly set this option, it defaults to `true` if the toolbox has categories, `false` otherwise.\n\nWork with block comments programmatically\n-----------------------------------------\n\nTo get a block comment: \n\n // Returns comment text or null if there is no comment.\n myBlock.getCommentText();\n\nTo set a block comment: \n\n // Sets comment text and displays comment icon.\n myBlock.setCommentText('My comment');\n\nTo delete a block comment: \n\n // Removes comment text and removes comment icon.\n myBlock.setCommentText(null);\n\nThe ability to work with comments programmatically is not affected by the state\nof the block (such as whether it is editable or collapsed) or the setting of the\n`comments` configuration option."]]