UPDATE:
Ok, I made a bit of a flow and it does show me the subtle differences between var
and let
.
[{"id":"863368aec398661f","type":"debug","z":"aaa025a7d2bb5b14","name":"debug 73","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1080,"y":2390,"wires":[]},{"id":"37d5b43fd4c7dbc1","type":"inject","z":"aaa025a7d2bb5b14","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":720,"y":2390,"wires":[["24ee0b11388d213a"]]},{"id":"24ee0b11388d213a","type":"function","z":"aaa025a7d2bb5b14","name":"LET 2","func":"let y = 1;\nif (y == 1) {\n let x = \"this is x\";\n}\nnode.warn(x);\nreturn msg;","outputs":1,"noerr":2,"initialize":"","finalize":"","libs":[],"x":870,"y":2390,"wires":[["863368aec398661f"]]},{"id":"16c28cb6e2408066","type":"function","z":"aaa025a7d2bb5b14","name":"VAR","func":"var x = \"this is x\";\nvar y = 1;\nif (y == 1) {\n node.warn(x);\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":870,"y":2460,"wires":[["a2721de0f2033386"]]},{"id":"ea7f22d533827b5b","type":"inject","z":"aaa025a7d2bb5b14","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":720,"y":2460,"wires":[["16c28cb6e2408066"]]},{"id":"a2721de0f2033386","type":"debug","z":"aaa025a7d2bb5b14","name":"debug 74","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1080,"y":2460,"wires":[]},{"id":"8b794dc1920420fe","type":"function","z":"aaa025a7d2bb5b14","name":"VAR 2","func":"var y = 1;\nif (y == 1) {\n var x = \"this is x\";\n}\nnode.warn(x);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":870,"y":2520,"wires":[["3c57e7a81925dc7a"]]},{"id":"5d1aa170893e0389","type":"inject","z":"aaa025a7d2bb5b14","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":720,"y":2520,"wires":[["8b794dc1920420fe"]]},{"id":"3c57e7a81925dc7a","type":"debug","z":"aaa025a7d2bb5b14","name":"debug 75","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1080,"y":2520,"wires":[]},{"id":"7d491892ce323b49","type":"function","z":"aaa025a7d2bb5b14","name":"LET","func":"let x = \"this is x\";\nlet y = 1;\nif (y == 1)\n{\n node.warn(x);\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":870,"y":2320,"wires":[["c00e6fbf46ad97b4"]]},{"id":"6f74944952c5411f","type":"inject","z":"aaa025a7d2bb5b14","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":720,"y":2320,"wires":[["7d491892ce323b49"]]},{"id":"c00e6fbf46ad97b4","type":"debug","z":"aaa025a7d2bb5b14","name":"debug 76","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1080,"y":2320,"wires":[]}]
It seems to confirm what I thought.
let
is better to make/force you to structure the code better.
If you want variables to be seen everywhere in the code, put them at the base of the code.
If you put them inside {
}they are only visible within that
{ }`.