Safely get a nested value inside an object

Today, I will share how could you safely get a nested value rather using hacks like this
const nestedVal = (
  obj &&
  obj.lorem &&
  obj.lorem.ipsum &&
  obj.lorem.ipsum.dolor
)
You could use code like this
function get(obj, path) {
    const valueFromPath = path.split('.').reduce((previousValue, pathItem) => {
        if (typeof previousValue === 'undefined') return; // Will finish the rest of the iterations unfortunately :(

        const nextValue = previousValue[pathItem];

        return nextValue;
    }, obj);

    return valueFromPath;
}

Pages