Where do we use $.extend() method in jQuery?


The jQuery.extend() method is used to merge contents of two or more objects together. The object is merged into the first object. 

You can try to run the following code to learn how to use extend() method −

Example

Live Demo

<!DOCTYPE html>
<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
  $("#button1").click(function() {
    var obj1 = {
      maths: 60,
      history: {pages: 150,price: 400,lessons: 30},
      science: 120
    };
    var obj2 = {
      history: { price: 150, lessons: 24 },
      economics: 250
    };
    $.extend(true, obj1, obj2);
    $("#demo").append(JSON.stringify(obj1));
   });
});
</script>
</head>
<body>
   <button id="button1">Result</button>
   <div id="demo"></div>
</body>
</html>

Updated on: 15-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements