Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                // Iterators example

const userNamesGroupedByLocation = {
  Tokio: [
    'Aiko',
    'Chizu',
    'Fushigi',
  ],
  'Buenos Aires': [
    'Santiago',
    'Valentina',
    'Lola',
  ],
  'Saint Petersburg': [
    'Sonja',
    'Dunja',
    'Iwan',
    'Tanja',
  ],
};

// We can't use arrow functions here because this wouldn't let us access the scope of the object ("this").
userNamesGroupedByLocation[Symbol.iterator] = function() {
  const cityKeys = Object.keys(this);
  let cityIndex = 0;
  let userIndex = 0;

  return {
    next: () => {
      // We already iterated over all cities
      if (cityIndex > cityKeys.length - 1) {
        return {
          value: undefined,
          done: true,
        };
      }

      const users = this[cityKeys[cityIndex]];
      const user = users[userIndex];
      

      const isLastUser = userIndex >= users.length - 1;
      if (isLastUser) {
        // Reset user index
        userIndex = 0;
        // Jump to next city
        cityIndex++
      } else {
        userIndex++;
      }

      return {
        done: false,
        value: user,        
      };
    },
  };
};

/*
const iterator = userNamesGroupedByLocation[Symbol.iterator]();

let state = iterator.next();
console.log(state.value, state.done);
state = iterator.next();
console.log(state.value, state.done);
state = iterator.next();
console.log(state.value, state.done);
state = iterator.next();
console.log(state.value, state.done);
state = iterator.next();
console.log(state.value, state.done);
state = iterator.next();
console.log(state.value, state.done);
state = iterator.next();
console.log(state.value, state.done);
state = iterator.next();
console.log(state.value, state.done);
state = iterator.next();
console.log(state.value, state.done);
state = iterator.next();
console.log(state.value, state.done);
state = iterator.next();
console.log(state.value, state.done);
state = iterator.next();
console.log(state.value, state.done);


for (let name of userNamesGroupedByLocation) {
 console.log('name', name);
}
*/

// Generator examples

function* nameGenerator() {
  yield 'Iwan';
  yield 'Aiko';
}

function* stringGenerator() {
  yield* nameGenerator();
  yield* ['one', 'two'];
  yield 'hi';
  yield 'hi';
  yield 'hi';
}

/*
const strings = stringGenerator();


console.log(strings.next());
console.log(strings.next());
console.log(strings.next());
console.log(strings.next());
console.log(strings.next());
console.log(strings.next());
console.log(strings.next());
*/

function* overrideValue() {
  const result = yield 'hi';
  console.log(result);
}

const overrideIterator = overrideValue();
overrideIterator.next();
overrideIterator.next('bye');


function* idGenerator() {
  let i = 0;
  while (true) {
    yield i++;
    console.log(i);
  }
}

/*
const ids = idGenerator();

console.log(ids.next().value);
console.log(ids.next().value);
console.log(ids.next().value);
console.log(ids.next().value);
console.log(ids.next().value);
*/

function* errorGenerator() {
  try {
    yield 'one';
    yield 'two';
  } catch(e) {
    console.error(e);
  }
}

const errorIterator = errorGenerator();

console.log(errorIterator.next()); 
console.log(errorIterator.throw('Bam!'));

// {value: 'one', done: false}
// Bam!
// {value: undefined, done: true}
              
            
!
999px

Console