
Play Store Application link β Java to JavaScript in 13 Steps – App on Google Play
Functions in JavaScript are like small machines that perform specific tasks. You can call these machines (functions) multiple times and they can use inputs (parameters) and produce outputs (return values). Scopes define where you can access certain variables in your code.
1. Function Declaration vs Function Expression
Function Declaration
This is like creating a named recipe that you can use anywhere in your code, even before you write the recipe.
Syntax:
function functionName(param1, param2, ...) {
// code to execute
return value;
}
Example Program:
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("John"); // Output: "Hello, John!"
Comparison to Java:
Similar to Javaβs method declaration:
void greet(String name) {
System.out.println("Hello, " + name + "!");
}
Function Expression
This is like assigning a recipe to a variable. You need to define it before you use it.
Syntax:
const functionName = function(param1, param2, ...) {
// code to execute
return value;
};
Example Program:
const add = function(a, b) {
return a + b;
};
let result = add(2, 3);
console.log(result); // Output: 5
Comparison to Java:
This is similar to using anonymous methods or lambda expressions:
Function<Integer, Integer> add = (a, b) -> a + b;
int result = add.apply(2, 3);
System.out.println(result); // Output: 5
2. Function Parameters and Arguments
Parameters are placeholders in your function definition. Arguments are the actual values you pass when calling the function.
Example Program:
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("John"); // Output: "Hello, John!"
Comparison to Java:
Parameters in Java:
void greet(String name) {
System.out.println("Hello, " + name + "!");
}
greet("John"); // Output: "Hello, John!"
3. Function Return Values
Functions can return a value, which can be used later. This is like getting the result of a recipe.
Example Program:
function add(a, b) {
return a + b;
}
let result = add(2, 3);
console.log(result); // Output: 5
Comparison to Java:
Returning values in Java:
int add(int a, int b) {
return a + b;
}
int result = add(2, 3);
System.out.println(result); // Output: 5
4. Scopes
Global Scope: Variables declared outside of functions are accessible anywhere in your code.
Local Scope: Variables declared inside a function are only accessible within that function.
Example Program:
let globalVar = "Hello, World!"; // Global variable
function printLocal() {
let localVar = "Hello, Local!"; // Local variable
console.log(globalVar); // Accessible here
console.log(localVar); // Accessible here
}
printLocal(); // Output: "Hello, World!" "Hello, Local!"
console.log(globalVar); // Output: "Hello, World!"
console.log(localVar); // Throws ReferenceError: localVar is not defined
Comparison to Java:
In Java, global variables are like static fields in a class, while local variables are like method variables.
Example in Java:
public class Main {
static String globalVar = "Hello, World!"; // Global variable
public static void printLocal() {
String localVar = "Hello, Local!"; // Local variable
System.out.println(globalVar); // Accessible here
System.out.println(localVar); // Accessible here
}
public static void main(String[] args) {
printLocal(); // Output: "Hello, World!" "Hello, Local!"
System.out.println(globalVar); // Output: "Hello, World!"
// System.out.println(localVar); // Compilation error: cannot find symbol
}
}
Understanding functions and scopes helps in organizing code efficiently. Functions allow you to reuse code, and scopes help manage where variables can be accessed. This makes your code more modular, maintainable, and easier to debug.