You can use the window.screen
object in JavaScript to detect the screen resolution of a user’s device. The screen
object has several properties that can be used to determine the screen resolution.
Here is an example of how to use the screen
object to detect the screen resolution:
let width = window.screen.width;
let height = window.screen.height;
console.log("Screen resolution: " + width + " x " + height);
In the above code, the screen.width
property returns the width of the screen in pixels, and the screen.height
property returns the height of the screen in pixels.
You can also use the window.innerWidth
and window.innerHeight
properties to get the dimensions of the browser window.
let width = window.innerWidth;
let height = window.innerHeight;
console.log("Window resolution: " + width + " x " + height);
Please note that window.screen.width
and window.screen.height
will give you the resolution of the device’s screen, while window.innerWidth
and window.innerHeight
will give you the resolution of the browser window.
Also, you can use the window.devicePixelRatio
to get the ratio of physical pixels to device-independent pixels which is a value that could be used to adjust your layout.
It’s worth noting that these properties work on all modern browsers, but they may not be supported on older browsers.
You can use any of these methods depending on your requirement.
Also Read:
- Difference Between encodeURIComponent() And encodeURI()
- Convert Comma Separated String Into An Array In JavaScript
- How To Check If A String Is Empty In JavaScript
- How To Sort An Array Of Numbers In JavaScript
- How To Return Multiple Values From A Function In JavaScript
- How To Get The Current URL With JavaScript
- How To Include A JavaScript File In Another JavaScript File
- How To Detect Screen Resolution With JavaScript
- How To parse JSON Into JavaScript
- How To Add Elements To An Array In JavaScript
- How To Generate A Timestamp In JavaScript
- How To Convert A JavaScript Object To JSON String
- How To Add An Element To The Beginning Of An Array
- How To Get The Value From The Input Field In JavaScript
- Adjust The iFrame Height To Fit With Content In JavaScript
- Call Two Functions From The Same onClick Event In JavaScript
- How To Detect When A Window Is Resized Using JavaScript
- How To Reset A Form With JavaScript
- How To Pass JavaScript Variables To PHP
- Difference Between PHP And JavaScript
- Armstrong Number In JavaScript
- How To Determine If A Number Is Odd Or Even In JavaScript
- How To Check If A Number Is A Palindrome In JavaScript
- How To Convert Strings To Uppercase In JavaScript
- How To Convert A String To Lowercase In JavaScript
- Code To Check If Age Is Not Less Than 18 Years In JavaScript
- How To Reverse A Number In JavaScript
- How To Check If A Number Is Prime Using JavaScript
- How To Find Factorial Of A Number In JavaScript
- Sum Of Two Numbers In JavaScript
- How To Display The Current Date And Time In JavaScript