# Homeyscript - const or var?

**URL:** <https://community.homey.app/t/homeyscript-const-or-var/79412>\
**Category:** Questions & Help\
**Created:** [April 17, 2023, 5:00pm UTC](https://community.homey.app/t/homeyscript-const-or-var/79412 "2023-04-17T17:00:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tiwas](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tiwas/32/10066_2.png) [@Tiwas](https://community.homey.app/u/Tiwas)\
**Post date:** [April 17, 2023, 5:00pm UTC](https://community.homey.app/t/homeyscript-const-or-var/79412/1 "2023-04-17T17:00:01Z")

</div>

Is there any real difference between using const and var (except for the obvious - one being constant and the other being variable) in homeyscript?

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [April 17, 2023, 5:27pm UTC](https://community.homey.app/t/homeyscript-const-or-var/79412/2 "2023-04-17T17:27:27Z")

</div>

Looks like that it is a real difference for me, constant or variable?

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [April 17, 2023, 5:32pm UTC](https://community.homey.app/t/homeyscript-const-or-var/79412/3 "2023-04-17T17:32:19Z")

</div>

Nice explanation here: [JavaScript Variables](https://www.w3schools.com/js/js_variables.asp)

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [April 17, 2023, 5:57pm UTC](https://community.homey.app/t/homeyscript-const-or-var/79412/4 "2023-04-17T17:57:43Z")

</div>

`var` is mostly deprecated, only in _very_ specific circumstances should you use them. Nowadays either use `const` or `let`.

---

<div class="post-metadata">

**Author:** ![Arie\_J\_Godschalk](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/arie_j_godschalk/32/63027_2.png) [@Arie\_J\_Godschalk](https://community.homey.app/u/Arie_J_Godschalk)\
**Post date:** [April 18, 2023, 3:30pm UTC](https://community.homey.app/t/homeyscript-const-or-var/79412/5 "2023-04-18T15:30:00Z")

</div>

A constant cannot be altered after setting.  
A var is changable, but defines the variable outside the direct scope, inside the function scope.  
A let is changeable, but defines a variable inside the scope.

See the answer/solution for the question here (because you might misuse var/let):

> <https://stackoverflow.com/questions/762011/what-is-the-difference-between-let-and-var>

---

<div class="post-metadata">

**Author:** ![Tiwas](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tiwas/32/10066_2.png) [@Tiwas](https://community.homey.app/u/Tiwas)\
**Post date:** [April 18, 2023, 3:54pm UTC](https://community.homey.app/t/homeyscript-const-or-var/79412/6 "2023-04-18T15:54:35Z")

</div>

Thanks! As let was new to me, the scoping was interesting.

Will using let also help with memory allocation? I guess it’s destroyed once out of scope?

---

<div class="post-metadata">

**Author:** ![Arie\_J\_Godschalk](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/arie_j_godschalk/32/63027_2.png) [@Arie\_J\_Godschalk](https://community.homey.app/u/Arie_J_Godschalk)\
**Post date:** [April 18, 2023, 5:22pm UTC](https://community.homey.app/t/homeyscript-const-or-var/79412/7 "2023-04-18T17:22:52Z")

</div>

(More simpler answer, yes the `let` itself is destroyed at the end of the current scope/block.)

Well, thats quite hard to explain, it depends on:

- What type is placed within the var/let.
- Which environment is is run in.
- How it is being referenced.

By variable i mean a `let` variable also in this post!

In most environement, for type number and boolean: Yes, it will be detroyed asap (during Garbage collection).  
Please understand that most (if not all, not sure) JavaScript engines are using variables always as reference: you are not really settings the variable. So if you set `letA` to 1, it actually creates a “hidden” memory part for the 1, and the `letA` references that 1. So if you set the `letA` to 2, the 1 still exists in memory, until garbage collection notices that the 1 is not referenced anymore, and removes it.  
So, clearing variables to empty values, is about the same as not referencing it in your code anymore, hardly no point in settings letA to undefined, unless you wanna use that variableNAME later on again.

However, for strings, it’s another story, especially for the (Google’s) V8 engine.

Lets say you place a large text in `let1`, about 100 charactes (not realy large i know, but enough to make the point).

Now, you take a `substring` of more than 12 characters (so, 13 or more) and place it in a `let2`, the `let2` is actually a (sub/partial) reference to `let1`.  
So, even tho you might not use and reference the `let1` anywhere in the continueing code, the **whole** `let1` will be kept in memory and is not garbage collected.  
So, be very carefull with strings and substrings, longer than 12 characters.  
Ofc, this is very speed and memory effecient when using and manipulating large text, when the substrings are also large.

However, it can also create massive memory leaks if you do not understand that part correctly.  
Example: Do not read complete websites, just to take the first 13 characters, because during the process and garbage collection, it will keep the whole website text, it will not see if it’s just a part and remove the rest.

Ofc, if you than not use the variables anymore, the text is removed from memory.  
But if you use scopes the way Angular(JS) uses in browsers, and append multiple 13-char-substring from differend large texts, and place the substring in the angular scope, you will notice **massive** memory increases, and it will not reduce at all.

I use a custom substring function for cases as these…

Hang on…

---

<div class="post-metadata">

**Author:** ![Arie\_J\_Godschalk](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/arie_j_godschalk/32/63027_2.png) [@Arie\_J\_Godschalk](https://community.homey.app/u/Arie_J_Godschalk)\
**Post date:** [April 18, 2023, 5:35pm UTC](https://community.homey.app/t/homeyscript-const-or-var/79412/8 "2023-04-18T17:35:33Z")

</div>

Here, this substring function, will make sure the new string is a completly new string.  
It will create a lott of strings in memory, but as soon as the garbage collection is done, only the resulting string is kept.  
If you do not reference the original string yourself anymore, even that one is deleted.  
It’s less performing (speedwise) as the normal substring when using it ofc, but it can, when used correctly, keep memory very low, making a lott of systems run faster in the end.

Only use this when **all** below point are true:

- When the original string is very large (depends on the moment and vision i guess).
- When the substring is 13 characters or more
- The JavaScript engine uses the “string mirror/reflection” (okay, the non-reflection is string slicing).

So, another example: For AVD/TFE templates, i read the Forum, and i only need a few parts, one is the urls, which are mostly longer that 12 characters  
For that purpose, this script reduced the memory extremly, keeping the app running much faster.

```auto
function substring(str, start, end) {
    return !str ? str : str.substring(start, end);
    if (!str) return str;
    let s = '', d, _end = (end || str.length);
    while (start < _end) {
        s += str.substring(start, start + (d = (d = _end - start) > 12 ? 12 : d));
        start += d;
    }
    return s;
}

```

---

<div class="post-metadata">

**Author:** ![Arie\_J\_Godschalk](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/arie_j_godschalk/32/63027_2.png) [@Arie\_J\_Godschalk](https://community.homey.app/u/Arie_J_Godschalk)\
**Post date:** [April 18, 2023, 5:40pm UTC](https://community.homey.app/t/homeyscript-const-or-var/79412/9 "2023-04-18T17:40:55Z")

</div>

A bit more information on the 12 char nostring\_slicing (can be configured in some cases):

- [“heap out of memory” because strings are kept in memory when 13 chars substrings are referenced · Issue #31891 · nodejs/node · GitHub](https://github.com/nodejs/node/issues/31891)

Not sure which parts are fixed or improved, but i did a lott of test for SimpleLog a few months back, and the used Node/Engine/Settings did use “substring Mirror/Reflect” way of substrings.  
So be carefull with substrings from large texts on HP 😉
