Friday, 10 October 2025

Rest and spread operator in javascript

 

Absolutely ๐Ÿ‘ — here are several practical and clear examples of the rest operator (...) in JavaScript, covering functions, arrays, and objects ๐Ÿ‘‡


๐Ÿงฎ 1. Rest in Function Parameters

When you don’t know how many arguments a function will receive:

function multiply(factor, ...numbers) { return numbers.map(n => n * factor); } console.log(multiply(2, 1, 2, 3, 4)); // Output: [2, 4, 6, 8]

๐Ÿ‘‰ factor gets the first argument (2), and ...numbers collects the rest into an array [1, 2, 3, 4].


๐Ÿง‘‍๐Ÿค‍๐Ÿง‘ 2. Rest with Array Destructuring

You can collect remaining array elements into a variable:

const fruits = ["apple", "banana", "mango", "orange"]; const [first, second, ...others] = fruits; console.log(first); // "apple" console.log(second); // "banana" console.log(others); // ["mango", "orange"]

๐Ÿ‘‰ The rest operator gathers all remaining elements after the first two.


๐Ÿ—ƒ️ 3. Rest with Object Destructuring

You can extract specific properties and collect the rest into a new object:

const person = { name: "Ayush", age: 25, country: "Nepal", profession: "Teacher" }; const { name, ...details } = person; console.log(name); // "Ayush" console.log(details); // { age: 25, country: "Nepal", profession: "Teacher" }

๐Ÿ‘‰ Here, ...details contains all other key-value pairs except name.


๐Ÿง‘‍๐Ÿซ 4. Rest in Arrow Functions

Useful in simple arrow functions that accept flexible parameters:

const sumAll = (...nums) => nums.reduce((a, b) => a + b, 0); console.log(sumAll(5, 10, 15)); // 30

๐Ÿ‘‰ The rest operator allows the function to accept any number of arguments.


๐Ÿงฉ 5. Combining Named and Rest Parameters

Mixing normal parameters with rest:

function show(first, ...rest) { console.log("First:", first); console.log("Rest:", rest); } show("A", "B", "C", "D"); // Output: // First: A // Rest: ["B", "C", "D"]



๐ŸŒŸ 1. Spread in Arrays

a. Copying an Array

const fruits = ["apple", "banana", "mango"]; const copy = [...fruits]; console.log(copy); // ["apple", "banana", "mango"]

๐Ÿ‘‰ The spread operator makes a shallow copy of the array (not just a reference).


b. Merging Arrays

const a = [1, 2, 3]; const b = [4, 5]; const combined = [...a, ...b]; console.log(combined); // [1, 2, 3, 4, 5]

๐Ÿ‘‰ It spreads elements of both arrays into one.


c. Adding Elements Easily

const nums = [2, 3, 4]; const newNums = [1, ...nums, 5]; console.log(newNums); // [1, 2, 3, 4, 5]

๐Ÿ‘‰ You can insert elements before and after spreading.


๐Ÿงฎ 2. Spread in Function Calls

When a function expects multiple arguments but you have them in an array.

const numbers = [10, 20, 30]; console.log(Math.max(...numbers)); // 30

๐Ÿ‘‰ The array is expanded into individual arguments.


๐Ÿง‘‍๐Ÿซ 3. Spread in Objects

a. Copying an Object

const student = { name: "Ayush", age: 25 }; const copyStudent = { ...student }; console.log(copyStudent); // { name: "Ayush", age: 25 }

๐Ÿ‘‰ Makes a shallow copy of an object.


b. Merging Objects

const personal = { name: "Ayush", age: 25 }; const contact = { email: "ayush@mail.com", country: "Nepal" }; const profile = { ...personal, ...contact }; console.log(profile); // { name: "Ayush", age: 25, email: "ayush@mail.com", country: "Nepal" }

๐Ÿ‘‰ Combines properties from multiple objects into one.


c. Overriding Properties

const user = { name: "Ayush", role: "student" }; const update = { role: "teacher" }; const newUser = { ...user, ...update }; console.log(newUser); // { name: "Ayush", role: "teacher" }

๐Ÿ‘‰ Later properties overwrite earlier ones.


4. Spread with Strings

You can spread a string into an array of characters.

const name = "Hello"; const letters = [...name]; console.log(letters); // ["H", "e", "l", "l", "o"]

๐Ÿ‘‰ Each character becomes a separate element.


⚖️ Summary Table

PurposeExampleExplanation
Copyingconst arr2 = [...arr1]Duplicates an array
Combining[...a, ...b]Joins arrays
Function argumentsMath.max(...nums)Expands array into arguments
Objects{...obj1, ...obj2}Merges or copies objects
Strings[...str]Converts string to characters

Thursday, 20 February 2025

APK and ADB

Morning:

I generated ADB files and APK file in react native.ADB files are geberated because ADB files are used deploy App in Playsore.I saw that apk files are bigger in size than adb files.The commands that i run to create that files:[]

we also need git installed in our computer when we try to crate apk or adb files using eas command.

Afternoon:

Taught trim function.

In mobile local storage data is in form of String so we convert in json using JSON.Parse() while rendering.

While saving in mobile storage we convert JSON data into string using JSON.Stringify().

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

Tuesday, 23 January 2024

Interface in Java Simple Explanation

 Certainly! Here are some questions related to Java interfaces:

  1. What is an interface in Java?

    • Answer: An interface in Java is a collection of abstract methods (methods without a body) and constant variables. It defines a contract for classes that implement it, specifying the methods they must provide.
  2. Can a class implement multiple interfaces in Java?

    • Answer: Yes, a class in Java can implement multiple interfaces. This feature is known as multiple inheritance through interfaces.
  3. What is the difference between an interface and an abstract class?

    • Answer: An interface can only contain abstract methods and constants, while an abstract class can have both abstract and concrete methods. A class can implement multiple interfaces, but it can extend only one abstract class.
  4. Can an interface have fields?

    • Answer: Yes, starting from Java 8, interfaces can have static final fields (constants) and default methods (methods with a default implementation).
  5. Explain the purpose of the default method in an interface.

    • Answer: A default method in an interface provides a default implementation for a method. It allows adding new methods to interfaces without breaking existing implementations.
  6. Can an interface extend another interface?

    • Answer: Yes, an interface in Java can extend one or more other interfaces. This is known as interface inheritance.
  7. What is the significance of the implements keyword in Java?

    • Answer: The implements keyword is used in Java to indicate that a class is implementing one or more interfaces. It establishes a contract between the class and the interfaces it implements.
  8. Can an interface have a constructor?

    • Answer: No, interfaces in Java cannot have constructors. They do not have instance variables, and any initialization is typically done using default or static methods.
  9. What is a functional interface?

    • Answer: A functional interface in Java is an interface that contains only one abstract method. It can have multiple default or static methods, but it must have only one abstract method to be considered a functional interface.
  10. How do you achieve multiple inheritance in Java using interfaces?

    • Answer: By implementing multiple interfaces in a class, you achieve multiple inheritance in Java. The class inherits the abstract methods from each interface it implements.

These questions cover various aspects of Java interfaces, including their definition, usage, features, and differences from other constructs like abstract classes. If you have any specific questions or need further clarification on any topic, feel free to ask!

Rest and spread operator in javascript

  Absolutely ๐Ÿ‘ — here are several practical and clear examples of the rest operator ( ... ) in JavaScript, covering functions, arrays, an...