C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Set add() methodThe JavaScript Set add() method is used to add a new object to the end of a WeakSet object. SyntaxThe add() method is represented by the following syntax: weakSetObj.add(value) Parametervalue - It represents the object to be added. ReturnThe WeakSet object. JavaScript Set add() method exampleHere, we will understand add() method through various examples. Example 1Let's see an example to add object to WeakSet object. <script> var ws = new WeakSet(); var obj1={}; var obj2={}; ws.add(obj1); ws.add(obj2); //Let's check whether the WeakSet object contains the added object document.writeln(ws.has(obj1)+"<br>"); document.writeln(ws.has(obj2)); </script> Output: true true Example 2In this example, we will see whether we add the objects to WeakSet object without initializing them. <script> var ws = new WeakSet(); try { ws.add(obj); document.writeln(ws.has(obj)); } catch(error) { document.writeln(error); } </script> Output: ReferenceError: obj is not defined
Next TopicWeakSet delete() Method
|