Skip to content
the poor coder

javascript basic function

Poor Man
Published
Reading time
1 min read
On this page

Javascript Basic Function#

As a web developer, it is important to have a good understanding of Javascript. One of the fundamental concepts in Javascript is functions. Functions are reusable blocks of code that perform a specific task.

There are two ways to define functions in Javascript:

  • Function declaration
  • Function expression

Function Declaration#

A function declaration is a statement that defines a named function.

Text

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

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

In the example above, we define a function called "greet" that takes one parameter "name". When we call the function with "Raju" as an argument, it prints "Hello, Raju!" to the console.

Function Expression#

A function expression is an anonymous function that is assigned to a variable.

Text

const greet = function(name) {
  console.log(`Hello, ${name}!`);
}

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

In the example above, we define an anonymous function and assign it to a variable called "greet". We then call the function with "Raju" as an argument, and it prints "Hello, Raju!" to the console.

One important thing to note is that function expressions are not hoisted like function declarations. This means that you must define the variable before calling the function.

Arrow Functions#

Arrow functions are a shorthand syntax for defining functions in Javascript. They were introduced in ES6.

Text

const greet = (name) => {
  console.log(`Hello, ${name}!`);
}

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

In the example above, we define an arrow function and assign it to a variable called "greet". We then call the function with "Raju" as an argument, and it prints "Hello, Raju!" to the console.

Arrow functions have some differences in behavior compared to regular functions. For example, they do not have their own "this" keyword and cannot be used as constructors. However, they are often more concise and easier to read.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC