7 Comments
author
Mar 8·edited Mar 8Author

Not exactly. const and var (now let), describe mutability vs immutability, important for memory efficiency. This is not the same as constant vs variable, which are programmatic concepts.

Constants are variables whose values are set at the beginning of a script. These values are not set programmatically during the execution of a script, nor do they ever change. They allow you to avoid hard coding values in your code by abstracting them as global values. By convention, constants are declared in all capital letters at the beginning of a piece of code. (Often these "constants" are environment variables, with the word variable in their name.) Never-the-less, algorithmically they are constant, and naming them in capital letters reminds programmers never to change them.

On the other hand, there is mutability vs immutability. This is largely irrelevant from a coding perspective, but important for speed and memory. Mutable and immutable objects are handled differently in memory, immutable objects being much more efficient in terms of allocation and access. JS made an unfortunate choice in choosing "const" to declare an object immutable. But JS uses const throughout code for values which are not known at the beginning of code execution, ie. a constant as described above. OTOH, these variables are immutable, and so you get an efficiency gain by declaring them so if you don't need to change them later.

Expand full comment

"Variables"

<proceeds to talk about const only...>

Well, no: const, like the name suggests, declares a constant (something that doesn't change), not a variable (something that does).

For variables there's "let" (or the deprecated "var").

Expand full comment

A million Thank Yous for this post. I am an Obsidian member who has v limited experience programming. I would love to learn JS but have had ++difficulty even organizing my pkm so that I even have a clean space to learn(ADHD is a helluva drug lol). Stumbling across this article and it's easy-to-read nature has given me lots of confidence not to give up!

Please never stop teaching! You have a great style and I will happily absorb anything you put forward to help those like me who have limited experience but are eager to learn :)

Expand full comment
author

I'm pleased you enjoyed it. I hope you keep going. Programming is fun and really useful.

Expand full comment

So if I wanted to include today's date on each daily note, I could include this in my template:

’’’dataviewjs

const today = moment(Date.now()).format("MMMM DD, YYYY")

dv.paragraph(`Today is ${today}`)

’’’

???

Expand full comment
author
Oct 11, 2023·edited Oct 11, 2023Author

Yes. But you might not want to for that purpose because if you open an old Daily Note you don't want it to show today's date, which it would.

Expand full comment
author
Oct 11, 2023·edited Oct 11, 2023Author

Elaborating a little: I'm using a canvas as a sort of rolling daily note, so in my case it would make sense because I don't need to look back at a prior version for any reason.

Expand full comment