DEV Community

Cover image for JavaScript,Method,Datatype
Neelakandan R
Neelakandan R

Posted on

JavaScript,Method,Datatype

What is JavaScript ?

JavaScript is the world's most popular programming language.
JavaScript is the programming language of the Web.JavaScript is easy to learn.

Why Study JavaScript?

JavaScript is one of the 3 languages all web developers must learn:

  1. HTML to define the content of web pages
  2. CSS to specify the layout of web pages
  3. JavaScript to program the behavior of web pages

What is function?

In JavaScript, a function is a block of code designed to perform a particular task. It allows you to reuse code, organize logic, and create modular applications.

1. Declaring a Function

function greet() {
  console.log("Hello, world!");
}
Enter fullscreen mode Exit fullscreen mode

2. Calling a Function


greet(); // Output: Hello, world!
Enter fullscreen mode Exit fullscreen mode

3. Function with Parameters

function add(a, b) {
  return a + b;
}

console.log(add(5, 3)); // Output: 8

Enter fullscreen mode Exit fullscreen mode

4. Function Expression

const multiply = function(x, y) {
  return x * y;
};

console.log(multiply(4, 5)); // Output: 20

Enter fullscreen mode Exit fullscreen mode

5.Why Functions Are Useful

Code reusability

Modular design

Easier debugging

Improved readability

JavaScript has 8 Datatypes

String
Number
Bigint
Boolean
Undefined
Null
Symbol
Object

In JavaScript, let, const, and var are used to declare variables, but they behave differently in terms of scope, hoisting, and mutability.

1. var (Old, avoid using it)

Function-scoped
Can be re-declared and updated
Hoisted (initialized as undefined)


function testVar() {
  var x = 10;
  if (true) {
    var x = 20; // same variable
    console.log(x); // 20
  }
  console.log(x); // 20
}
Enter fullscreen mode Exit fullscreen mode

2. let (Modern, preferred for variables that change)

Block-scoped
Can be updated, but not re-declared in the same scope
Hoisted, but not initialized (accessing before declaration gives error)

function testLet() {
  let y = 10;
  if (true) {
    let y = 20; // different variable
    console.log(y); // 20
  }
  console.log(y); // 10
}
Enter fullscreen mode Exit fullscreen mode

3. const (Modern, for constants)

Block-scoped
Cannot be updated or re-declared
Must be initialized at declaration
For objects/arrays, the reference is constant (you can still change properties)

const z = 30;
// z = 40; ❌ Error: Assignment to constant variable

const person = { name: "Alice" };
person.name = "Bob"; // βœ… Allowed: changing property
Enter fullscreen mode Exit fullscreen mode

In JavaScript, the **return keyword is used inside a function to:**
Stop the function execution.
Send a value back to the place where the function was called.

Basic Syntax

function add(a, b) {
  return a + b;
}

let result = add(5, 3);  // result = 8
console.log(result);     // Output: 8

Enter fullscreen mode Exit fullscreen mode

parameters and arguments

In JavaScript, parameters and arguments are related to how functions receive data. They’re often confused, but they mean different things:

Parameters

These are placeholders in the function definition.
They define what kind of input the function can take.

function greet(name) { // 'name' is a parameter
  console.log("Hello, " + name);
}
Enter fullscreen mode Exit fullscreen mode

Arguments

These are the actual values you pass when calling the function.

greet("Alice"); // "Alice" is an argument
Enter fullscreen mode Exit fullscreen mode

Example with Multiple Parameters and Arguments

function add(a, b) { // a and b are parameters
  return a + b;
}

let sum = add(5, 3); // 5 and 3 are arguments
console.log(sum); // Output: 8


Enter fullscreen mode Exit fullscreen mode

Top comments (0)