Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to use the for…of syntax for a for loop in observablehq?

In regular JavaScript, one can write a for loop using the for…of syntax. Is this possible with observablehq?

const array1 = [1, 2, 3];

for (const element of array1) {
  console.log(element);
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Not really. As explained in the tutorials, Observable is not [exactly] JavaScript.

In your case, you need to do two things:

First, there is no const, just make a cell with array1 value:

array1 = [1, 2, 3];

Then for the for loop, in a separate cell, put it between curly braces:

{
    for (const element of array1) {
        console.log(element);
    }
}

That cell, of course, will return undefined:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading