JavaScript with Statement Last Updated : 29 Jan, 2024 Comments Improve Suggest changes Like Article Like Report The with statement in JavaScript allows for simplified access to object properties by establishing a default object scope. While it can reduce code repetition, it is generally discouraged due to potential confusion and negative impacts on code maintainability and performance. Syntax:with (object) { // Statement(s) that access properties of `object`}Parameters:object: It refers to any expression that can be evaluated as an object. When you use braces the first thing it does is search, for any identifier within the object, before checking the usual scope chain. Why We Avoid using with statement ?Please note that while the, with statement in JavaScript is still supported by browsers it is highly recommended to avoid using it. This recommendation is based on three concerns: Scope ambiguity: There can be confusion and hidden bugs when there is uncertainty, about access within the block. Performance impact: In codebases performance can be affected by lookups, within the with object. Security vulnerabilities: Using the with statement can introduce some kind security vulnerabilities by accessing properties, which poses potential risks to security. Example 1: The below example will access properties within a nested object structure without repeatedly using dot notation. JavaScript const GFG = { designation: "Writer", details: { name: "Pankaj", age: 20, }, }; with (GFG.details) { console.log(`${name} ${age}`); } OutputPankaj 20 Example 2: The below code modifies the properties of an object passed as an argument to a function. JavaScript function GFG(user) { with (user) { name = "Pankaj"; age += 2; address.city = "Surat"; } } const myUser = { name: "Neeraj", age: 18, address: { city: "Prayagraj" } }; GFG(myUser); console.log(myUser); Output{ name: 'Pankaj', age: 20, address: { city: 'Surat' } } Comment More infoAdvertise with us Next Article JavaScript with Statement P pankajbind Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Statements Similar Reads JavaTuples with() method The with() method in org.javatuples is used to instantiate a tuple in a semantically elegant way, with the values given as parameters. This method can be used for any tuple class object of the javatuples library. This method is a static function in each javatuple class and it returns the tuple class 3 min read Year with() Method in Java with Examples In Year class, there are two types of with() method depending upon the parameters passed to it. with(TemporalAdjuster adjuster) with(TemporalAdjuster adjuster) method of the Year class used to adjusted this Year using TemporalAdjuster and after adjustment returns the copy of adjusted Year.The adjust 3 min read LocalDate with() Method in Java with Examples In LocalDate class, there are two types of with() method depending upon the parameters passed to it. with(TemporalAdjuster adjuster) with(TemporalAdjuster adjuster) method of the LocalDate class used to adjusted this date-time using TemporalAdjuster passed as parameter and after adjustment returns t 3 min read MonthDay with() Method in Java with Examples with(Month month) method of the MonthDay class used to alter the month-of-year of MonthDay object using month passed as a parameter and after that method returns the copy of altered MonthDay.If the day-of-month value is invalid for the specified month after altering operation, the day will be adjust 2 min read YearMonth with() Method in Java with Examples In YearMonth class, there are two types of with() method depending upon the parameters passed to it. with(TemporalAdjuster adjuster) with(TemporalAdjuster adjuster) method of the YearMonth class used to adjusted this YearMonth using TemporalAdjuster and after adjustment returns the copy of adjusted 3 min read Like