Arrow functions were introduced in JavaScript's ES6 version. They are a shorter way to write functions. You don't need to use the word 'function' anymore, and instead of using the word 'function', you use an arrow (=>).
// Arrow function with two inputs const sum = (firstNumber, secondNumber) => { return firstNumber + secondNumber; }; console.log(sum(2,5)); // Shows: 7 // Arrow function with no inputs const sayHello = () => { console.log('hello'); }; sayHello(); // Shows: hello // Arrow function with one input const showWeight = weight => { console.log(`Weight: ${weight} kg.`); }; showWeight(25); // Shows: Weight: 25 kg. // Shorter arrow function const multiply = (a, b) => a * b; console.log(multiply(2, 30)); // Shows: 60
There are different ways to write functions in JavaScript. One common way is by using the 'function' keyword.
// Defining the function: function sum(num1, num2) { return num1 + num2; } // Using the function: console.log(sum(3, 6)); // Shows: 9
A function is a set of instructions that you can reuse. Functions can take in inputs and give back an output. You can name a function, or you can create one without a name, which is called an anonymous function.
// Named function function rocketToMars() { return 'BOOM!'; } // Anonymous function const rocketToMars = function() { return 'BOOM!'; }
A function expression is when you create a function as part of an expression, like assigning it to a variable.
const dogSound = function() { return 'Woof!'; }; console.log(dogSound()); // Shows: Woof!
When you create a function, you can set up placeholders called parameters. These are like empty boxes that get filled when the function is used. When the function runs, it uses the values you put into those boxes.
// The parameter is name function sayHello(name) { return `Hello, ${name}!`; } console.log(sayHello('Alice')); // Shows: Hello, Alice!
Functions can send back a result using the return keyword. If you don't include return, the function won't send anything back, and it will return 'undefined' by default.
// Using return function sum(num1, num2) { return num1 + num2; } console.log(sum(2, 4)); // Shows: 6 // Without return function sumWithoutReturn(num1, num2) { num1 + num2; } console.log(sumWithoutReturn(2, 4)); // Shows: undefined
A function declaration is when you create a function with the 'function' keyword. This type of function can be used before it's even declared in the code.
function add(num1, num2) { return num1 + num2; } console.log(add(5, 10)); // Shows: 15
To use a function, you 'call' it by using its name followed by parentheses. If the function needs inputs, you put them inside the parentheses.
// Defining the function function sum(num1, num2) { return num1 + num2; } // Calling the function console.log(sum(2, 4)); // Shows: 6
Scope is all about where in the code you can use a particular variable or function.
function myFunction() { var carName = "Tesla"; // Code here can use carName } // Code here can't use carName
A block is a chunk of code within curly braces {}. Variables defined with const or let inside these blocks can only be used inside that block. They can't be accessed outside the block.
const isLoggedIn = true; if (isLoggedIn == true) { const statusMessage = 'User is logged in.'; } console.log(statusMessage); // Error: statusMessage is not defined
Global variables are accessible from anywhere in the code because they are not limited to a block. They are usually defined outside any function or block.
// Variable declared globally const color = 'blue'; function printColor() { console.log(color); } printColor(); // Shows: blue
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!