Saturday, January 11, 2025

Arrow function in JavaScript

*Introduction*


Arrow functions are a concise way to write functions in JavaScript. They were introduced in ECMAScript 2015 (ES6) and have since become a popular choice among developers. In this article, we'll explore the basics of arrow functions, their syntax, and how they differ from traditional functions.


*Syntax*


The syntax for an arrow function is as follows:


```

const functionName = (parameters) => {

  // function body

}

```


Here, `functionName` is the name of the function, `parameters` is a comma-separated list of parameters, and `function body` is the code that gets executed when the function is called.


*Example*


Here's an example of a simple arrow function:


```

const greet = (name) => {

  console.log(`Hello, ${name}!`);

}


greet("John"); // Output: Hello, John!

```


*Concise Syntax*


One of the benefits of arrow functions is their concise syntax. If the function body consists of only one statement, you can omit the brackets and the `return` keyword:


```

const double = (x) => x * 2;

console.log(double(5)); // Output: 10

```


*Implicit Return*


Arrow functions also support implicit return. If the function body is an expression, the result of that expression is returned automatically:


```

const sum = (a, b) => a + b;

console.log(sum(2, 3)); // Output: 5

```


*This Context*


Unlike traditional functions, arrow functions do not have their own `this` context. Instead, they inherit the `this` context from the surrounding scope:


```

const person = {

  name: "John",

  greet: () => {

    console.log(`Hello, my name is ${this.name}`);

  }

}


person.greet(); // Output: Hello, my name is undefined

```


In this example, the `greet` function does not have its own `this` context, so `this.name` is `undefined`. To fix this issue, you can use a traditional function or bind the `this` context explicitly:


```

const person = {

  name: "John",

  greet: function() {

    console.log(`Hello, my name is ${this.name}`);

  }

}


person.greet(); // Output: Hello, my name is John

```


*Conclusion*


Arrow functions are a powerful feature in JavaScript that can simplify your code and make it more concise. They offer a more expressive syntax and can be used in a variety of situations, from simple functions to complex callbacks. However, it's essential to understand the differences between arrow functions and traditional functions, especially when it comes to the `this` context.

Tuesday, November 7, 2017

Writting a shell script file from linux command shell

$ echo '#!/bin/sh' > shell_from_cmd.sh
$ echo 'echo my firt script' >> shell_from_cmd.sh
$ chmod 755 my-script.sh
$ ./my-script.sh
my firt script
$

Saturday, March 7, 2015

Ouput of the program part 2

 Q. What is the output of following program?

#include <iostream>
using namespace std;

// main() is where program execution begins.
int g,t;
float f,h;
int main()
{
   int a,b;
   float d,l;
cout<<"GLobal Int g="<<g<<endl;
cout<<"GLobal Int t="<<t<<endl;
cout<<"Local Int a="<<a<<endl;
cout<<"Local Int b="<<b<<endl;
cout<<"Local float d="<<d<<endl;
cout<<"Local float l="<<l<<endl;
cout<<"Global float f="<<f<<endl;
cout<<"Global float h="<<h<<endl;
   return 0;
}

Answer:-

GLobal Int g=0    //always 0 if global int 
GLobal Int t=0    //always 0 if global int 
Local Int a=0    //any garbage value if local and not assigned , in most case it can take 0 if only one int or one float
Local Int b=4196896   //any garbage value if local and not assigned 
Local float d=0  //any garbage value if local and not assigned 
Local float l=5.88211e-39   //any garbage value if local and not assigned 
Global float f=0    //always 0 if global float 
Global float h=0    //always 0 if global float 


Explanation :
Local variable is not defined by the system and need to be initialized. I not initialized then it can take any garbage value but the Global variables are initialized automatically by the system when we define them.

Tuesday, December 30, 2014

Programing Tricks ----Part1

Ques 1 We all know that a function cant return more than one value. So for modifying the original values we use pointers and thus can use the values accordingly in main. But if we do not want to use pointer then what will you use??

Answer-- we will use structure in that case

for example:-

 #include<stdio.h>
 struct marks{
     int maths;
     int physics;
     int chem;
    
 };
 struct marks deviation(struct marks student , struct marks student2 );
 int main(){
        
         struct marks student1;
         student1.maths= 87;
         student1.chem = 67;
         student1.physics=96;
        
         struct marks avg;
         avg.maths= 55;
         avg.chem = 45;
         avg.physics=34;
         //struct marks dev;
         struct marks dev= deviation(student1 , avg );
         printf("%d %d %d" ,dev.maths,dev.chem,dev.physics);

     return 0;
 }
struct marks deviation(struct marks student , struct marks student2 ){
     struct marks dev;
     dev.maths = student.maths-student2.maths;
     dev.chem = student.chem-student2.chem;
    dev.physics = student.physics-student2.physics;   
     return dev;
 }

Saturday, December 27, 2014

Programming basic questions Part-2

1. Can we define a function with arguments as pointer as well as value??

I mean if a swap funcation is defined as :-

void swap(int num1 ,int*num2 );

is it correct to define like this??

Ans :-

Yes, we can. there is no issue in this case but as we know that the function does not allow to return values more than one. thus doing this wont change the value of num1 in the calling function and our main objective of swapping will remain incomplete.

Thursday, August 7, 2014

Enumerated type(ENUMs)

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Because they are constants, the names of an enum type's fields are in uppercase letters.

In the Java programming language, you define an enum type by using the enum keyword. For example, you would specify a days-of-the-week enum type as:


public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY
}

Monday, July 21, 2014

Print format string : printf

printf function as we know is used to give output on the screen,but while using so many times this function, I stuck at a point that why we use it in this way only ?


printf actually means print format string which helps us to print any variable whether it is integer float  character  or string. but what we need to do is wehave to tell this that we want to print particular type of variable in our statement.

printf undersatnds it and then converts that variable into string to give output as a statement.

for example:--
int num= 3;
float rate = 3.2;
char letter = 'A';

printf("%d, %f, %c" num, rate, letter);

output:--

3,3.2,A

for further information refer to this article.