- This article or section documents the current version of Scratch (version 3.0). For this article in Scratch 1.4, see Casting (1.4).
Casting is the conversion of data types for compatibility within methods. For example, a list is cast to a string whenever it is used as a string input. For example, joining a list ["apple", "banana", "cantaloupe"] to "... is a list" will return the string "apple banana cantaloupe... is a list".
Rules
Casting in Scratch follows the following rules:
Casting Rules | String input | Number input | |
---|---|---|---|
Strings | String: "abc" |
"abc" | 0 |
String: "123" |
"123" | 123 | |
Numbers | Number: 123 |
"123" | 123 |
Booleans | Boolean: true |
"true" | 1 |
Boolean: false |
"false" | 0 | |
Lists | List: ["a", "b", "c"] |
"abc" | 0 |
List: ["a", "b", "c", "are the letters"] |
"a b c are the letters" | 0 | |
List: ["1", "2", "3"] |
"123" | 123 | |
List: [1, 2, 3] |
"1 2 3" | 0 | |
List: [1, 22, 3] |
"1 22 3" | 0 | |
List: ["3", ".", "1", "4"] |
"3.14" | 3.14 | |
List: [3, ".", 1, 4] |
"3 . 1 4" | 0 | |
List: [] |
"" | 0 |