Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Year with() Method in Java with Examples
Next article icon

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}`); } 

Output
Pankaj 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' } } 

Next Article
Year with() Method in Java with Examples
author
pankajbind
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
  • LocalTime with() Method in Java with Examples
    In LocalTime class, there are two types of with() method depending upon the parameters passed to it. with(TemporalAdjuster adjuster) with(TemporalAdjuster adjuster) method of the LocalTime class used to adjusted this time using TemporalAdjuster and after adjustment returns the copy of adjusted time.
    3 min read
  • YearMonth withYear() method in Java with Examples
    withYear(int year) method of the YearMonth class used to alter the year of YearMonth object using year passed as a parameter and after that method returns the copy of altered YearMonth. This instance is immutable and unaffected by this method call. Syntax: public YearMonth withYear(int year) Paramet
    2 min read
  • LocalDateTime with() Method in Java with Examples
    In LocalDateTime class, there are two types of with() method depending upon the parameters passed to it. with(TemporalAdjuster adjuster) with(TemporalAdjuster adjuster) method of the LocalDateTime class used to adjusted this date-time using TemporalAdjuster and after adjustment returns the copy of a
    3 min read
  • Java Stream & Lambda Expression Coding Practice Problems
    Java Streams and Lambda Expressions provide a powerful way to perform functional-style operations on collections. This collection of Java Stream and Lambda Expression practice problems covers fundamental concepts, from writing simple lambda expressions to performing complex stream operations like fi
    2 min read
  • LocalDate withYear() method in Java with Examples
    The withYear() method of LocalDate class in Java returns a copy of this LocalDate with the year altered. Syntax: public LocalDate withYear(int year) Parameter: This method accepts a mandatory parameter year which specifies the year to set in the result which can be in the range from MIN_YEAR to MAX_
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences