get device orientation | Javascript

 
function getOrientation() {  
    const isPortrait = screen.orientation.type.startswith('portrait')  
    return isPortrait ? 'portrait' : 'landscape'
}
 

AI Explanation

This JavaScript function, `getOrientation`, determines whether the device's orientation is in portrait or landscape mode. 1. `screen.orientation.type`: This property of the `screen` object provides information about the orientation of the device. 2. `startswith('portrait')`: This method is used to check if the orientation type starts with the string 'portrait'. 3. The function `getOrientation` first checks if the device's orientation starts with 'portrait' using the `startswith` method. 4. If the orientation starts with 'portrait', it sets the `isPortrait` variable to `true`, else it sets it to `false`. 5. The function returns 'portrait' if the device is in portrait mode (`isPortrait` is `true`) and 'landscape' if it is not (`isPortrait` is `false`). Overall, this function is used to determine whether the device is in portrait or landscape mode based on the screen orientation information provided by the browser.