@jgCarroll/

stringsAndArrays

JavaScript

A common interview question

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const data1 = 'Jordan Smith';
 
const data2 = [].filter.call(data1, function(elem, index) {
  return index > 6;
});
 
console.log(data2); // logs [ 'S', 'm', 'i', 't', 'h' ]

// using array methods on a string

// can only use 'read-only' methods: e.g., filter, forEach, map, some, every, etc.

// can't use: push, pop, splice, shift, reverse, etc.

// Note that [].filter is a shortcut for Array.prototype.filter

// the call() method sets the this context to data1
Native Browser JavaScript