JavaScript Screen orientation Property

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

Screen.orientation is a read-only instance property that returns a ScreenOrientation object for the current screen orientation. Learn type and angle, the MDN landscape/portrait switch, how to listen for rotation with change, and how this differs from older APIs—with five examples and try-it labs.

01

Kind

Read-only property

02

Returns

ScreenOrientation

03

Access

window.screen

04

Key fields

type, angle

05

Event

change

06

Status

Baseline widely

Introduction

Phones and tablets rotate. Games and video UIs often want to know whether the screen is in portrait or landscape—and which way is “up.” The Screen Orientation API exposes that through window.screen.orientation.

The property itself is an object (ScreenOrientation), not a plain string. Beginners usually start with screen.orientation.type, which returns values like "landscape-primary" or "portrait-primary".

JavaScript
console.log(window.screen.orientation);
console.log(screen.orientation.type);
💡
Beginner tip

MDN notes that older, prefixed versions returned a string equivalent to ScreenOrientation.type. Modern code should treat screen.orientation as an object and read .type.

Understanding the Property

MDN: the orientation read-only property of the Screen interface returns the current orientation of the screen as a ScreenOrientation instance.

  • Instance — on window.screen.
  • Read-only property — you do not assign a new orientation object to it.
  • Object value — use .type, .angle, and events.
  • Baseline — Widely available (MDN: since March 2023).

📝 Syntax

JavaScript
const orient = window.screen.orientation;
// same as:
const orient2 = screen.orientation;

console.log(orient.type);
console.log(orient.angle);

Value

An instance of ScreenOrientation representing the orientation of the screen.

🎨 Common type Values

typeBeginner meaning
portrait-primaryUsual upright portrait
portrait-secondaryPortrait, flipped the other way
landscape-primaryUsual landscape
landscape-secondaryLandscape, flipped (often “upside down”)

angle is a number of degrees of rotation (commonly 0, 90, 180, or 270, depending on the device).

🎯 MDN Landscape / Portrait Switch

MDN’s classic teaching example branches on screen.orientation.type:

JavaScript
switch (screen.orientation.type) {
  case "landscape-primary":
    console.log("That looks good.");
    break;
  case "landscape-secondary":
    console.log("Mmm… the screen is upside down!");
    break;
  case "portrait-secondary":
  case "portrait-primary":
    console.log("Mmm… you should rotate your device to landscape");
    break;
  default:
    console.log("The orientation API isn't supported in this browser :(");
}

🔄 Listening for Orientation Changes

When the user rotates the device, ScreenOrientation fires a change event. Re-read type / angle inside the handler.

JavaScript
if (screen.orientation) {
  screen.orientation.addEventListener("change", () => {
    console.log("now:", screen.orientation.type, screen.orientation.angle);
  });
}
📌
Locking note

ScreenOrientation.lock() / unlock() can request a fixed orientation (for example fullscreen games), but browsers often require fullscreen or user activation and may reject the promise. Prefer reading orientation first; treat locking as an advanced, optional step.

⚡ Quick Reference

GoalCode
Get orientation objectscreen.orientation
Read type stringscreen.orientation.type
Read anglescreen.orientation.angle
On rotateorientation.addEventListener("change", …)
MDN statusBaseline Widely available (Mar 2023)

🔍 At a Glance

Four facts about screen.orientation.

Kind
property

Read-only

Type
ScreenOrientation

Object

Via
window.screen

Screen object

Status
baseline

Widely available

Examples Gallery

Examples follow MDN Screen.orientation. Rotate a phone or resize a desktop window (when the browser reports a type) to see values change.

📚 Getting Started

Read the orientation object and practice the MDN switch.

Example 1 — Read orientation.type

Log the ScreenOrientation object and its type string.

JavaScript
if (screen.orientation) {
  console.log(screen.orientation.type);
  console.log(typeof screen.orientation.type);
} else {
  console.log("screen.orientation not available");
}
Try It Yourself

How It Works

Feature-detect screen.orientation, then read .type. Your string depends on how the device is held.

Example 2 — MDN Type Switch

Branch messages for landscape vs portrait (from MDN).

JavaScript
if (!screen.orientation) {
  console.log("The orientation API isn't supported in this browser :(");
} else {
  switch (screen.orientation.type) {
    case "landscape-primary":
      console.log("That looks good.");
      break;
    case "landscape-secondary":
      console.log("Mmm… the screen is upside down!");
      break;
    case "portrait-secondary":
    case "portrait-primary":
      console.log("Mmm… you should rotate your device to landscape");
      break;
    default:
      console.log("The orientation API isn't supported in this browser :(");
  }
}
Try It Yourself

How It Works

Directly inspired by MDN. Portrait cases share one message; landscape-secondary is called out as upside down.

📈 Angle, Change Events & Snapshots

Combine type with angle and react to rotation.

Example 3 — Type Plus Angle

Log both common ScreenOrientation fields together.

JavaScript
if (!screen.orientation) {
  console.log("unsupported");
} else {
  console.log({
    type: screen.orientation.type,
    angle: screen.orientation.angle
  });
}
Try It Yourself

How It Works

type is the friendly OrientationType string; angle is the numeric rotation. Use both in diagnostics panels.

Example 4 — Listen for change

Print the current type, then update when orientation changes.

JavaScript
function report() {
  if (!screen.orientation) {
    return "unsupported";
  }
  return screen.orientation.type + " @ " + screen.orientation.angle + "°";
}

console.log("now:", report());

if (screen.orientation) {
  screen.orientation.addEventListener("change", () => {
    console.log("changed:", report());
  });
}
Try It Yourself

How It Works

The first log runs immediately. Rotate a mobile device to see the change handler fire in the try-it lab console / page output flow.

Example 5 — Orientation Plus Screen Size Snapshot

Combine orientation with common Screen size metrics.

JavaScript
console.log({
  type: screen.orientation ? screen.orientation.type : "(unavailable)",
  angle: screen.orientation ? screen.orientation.angle : null,
  width: screen.width,
  height: screen.height,
  availWidth: screen.availWidth,
  availHeight: screen.availHeight
});
Try It Yourself

How It Works

After a rotate, width / height often swap roles relative to the physical display. Pair orientation with height for fuller diagnostics.

🚀 Common Use Cases

  • Prompt users to rotate to landscape for video or games.
  • Adjust layout hints when type is portrait vs landscape.
  • Log orientation in mobile QA and device diagnostics.
  • Listen for change to refresh UI after a rotate.
  • Teaching ScreenOrientation as the modern alternative to old window.orientation.

🔧 How It Works

1

Device reports rotation

Sensors / OS know how the screen is oriented.

OS
2

Browser builds ScreenOrientation

Exposes type, angle, change, and optional lock APIs.

Screen
3

You read screen.orientation

Branch on type or listen for change.

Read
4

Adapt the experience

Hints, layout tweaks, or diagnostics—still use CSS for most layout.

📝 Notes

  • Baseline Widely available (MDN, since March 2023).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Property is read-only; value is a ScreenOrientation object.
  • Prefer screen.orientation over legacy window.orientation.
  • Related: mozEnabled, height, Window hub, JavaScript hub.

Universal Browser Support

Screen.orientation is Baseline Widely available across modern browsers (MDN: since March 2023). Logos use the shared browser-image-sprite.png sprite from this project. Always read .type on the ScreenOrientation object.

Baseline · Widely available

Screen.orientation

Read-only ScreenOrientation — current type and angle of the screen.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer No modern Screen.orientation object
Unavailable
orientation Excellent

Bottom line: Use screen.orientation.type for portrait/landscape strings, listen for change on rotate, and keep CSS media/orientation queries as the primary layout tool.

Conclusion

window.screen.orientation returns a ScreenOrientation object for the current screen orientation. Read type and angle, use the MDN switch for landscape vs portrait messaging, and listen for change when the device rotates.

Continue with pixelDepth, height, Window methods, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Read screen.orientation.type for portrait/landscape strings
  • Feature-detect screen.orientation before using it
  • Listen for change when UI should update on rotate
  • Keep responsive CSS as the main layout tool
  • Treat lock/unlock as optional and permission-sensitive

❌ Don’t

  • Assign to screen.orientation (the property is read-only)
  • Assume older prefixed APIs still return a bare string
  • Prefer legacy window.orientation for new apps
  • Rely only on JS orientation for critical CSS layout
  • Expect orientation lock to succeed without fullscreen / policy checks

Key Takeaways

Knowledge Unlocked

Five things to remember about orientation

ScreenOrientation for current screen rotation.

5
Core concepts
🔄 02

Object

ScreenOrientation

Value
📱 03

type

portrait / landscape

Field
📈 04

change

on rotate

Event
🎯 05

Baseline

widely available

Status

❓ Frequently Asked Questions

A read-only ScreenOrientation object describing the current screen orientation. Access it as window.screen.orientation. Use orientation.type for the string (for example landscape-primary).
No. MDN marks Screen.orientation as Baseline Widely available (since March 2023). It is not Deprecated, Experimental, or Non-standard.
Common OrientationType strings: portrait-primary, portrait-secondary, landscape-primary, and landscape-secondary.
window.orientation is an older, non-standard number API. Prefer screen.orientation (ScreenOrientation) for modern code.
The Screen.orientation property itself is read-only. You read the ScreenOrientation object; locking/unlocking uses ScreenOrientation methods when allowed, not assignment to screen.orientation.
Listen for the change event on screen.orientation (ScreenOrientation), then read type/angle again.
Did you know?

CSS also has orientation media features (for example @media (orientation: landscape)). Use CSS for most layout changes, and screen.orientation when JavaScript needs the precise OrientationType string or angle.

Next: Screen.pixelDepth

Learn the screen bit depth property (often reported as 24).

pixelDepth →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful