Maharashtra's Most Trusted

with more than 0 members

Chapter 2 - Array, Function & String

Part 2

Define Function -: 

In JavaScript, a function is a block of code designed to perform a particular task. It is executed when “called” or “invoked.” Functions help you organize your code, make it reusable, and structure it logically.

1. Function Declaration

This is the most common way to define a function. It has the following syntax:

  • functionName: This is the name you give the function, which is used to call or invoke it.
  • parameters: These are placeholders for values the function can accept when called.
  • code to be executed: The block of code that runs when the function is invoked.

Example:

2. Function Invocation

Once a function is defined, you can call it (or invoke it) by using its name followed by parentheses.

Example:

 

3. Return Statement

The return statement is used in a function to send a value back to the caller. Once a return statement is executed, the function stops running.

Example:

Function Calling -: 

In JavaScript, calling a function means executing the code within the function block. You do this by specifying the function’s name followed by parentheses, optionally passing arguments if the function takes parameters.

Syntax of Calling a Function

To call or invoke a function, you use its name followed by parentheses.

Example of Calling a Function

 

Function Calling through HTML-: 

In HTML, you can call JavaScript functions through various event attributes. These event attributes are typically tied to user interactions like clicks, key presses, form submissions, or even page load events. JavaScript functions can be invoked directly from HTML by using these attributes in the HTML tags.

Here’s a step-by-step guide with examples:

1. Calling a JavaScript Function on Button Click

You can call a JavaScript function when a button is clicked by using the onclick event handler in HTML.

String in Java Script -: 

In JavaScript, a string is a sequence of characters used to represent text. Strings are one of the primitive data types in JavaScript, and they are immutable, meaning once created, they cannot be changed (although new strings can be created based on operations performed on existing ones).

1. Declaring Strings

Strings in JavaScript can be declared in three ways:

  • Using single quotes (').
  • Using double quotes (").
  • Using backticks ( ) for template literals (introduced in ES6).

Example:


All of these declarations create valid string variables. The choice between single or double quotes is typically based on preference or the need to include quotes within the string itself.

2. String Properties

  • Length Property: The .length property returns the number of characters in the string.

Example:


3. String Methods

JavaScript provides many built-in methods to work with strings. Some of the most common methods include:

3.1. charAt()

Returns the character at a specific index.


3.2. indexOf()

Returns the index of the first occurrence of a substring. If the substring is not found, it returns -1.


3.3. slice()

Extracts a portion of a string and returns it as a new string without modifying the original string.


3.4. substring()

Similar to slice(), but it doesn’t accept negative indices.


3.5. toUpperCase() and toLowerCase()

Converts the string to all uppercase or lowercase letters.


3.6. replace()

Replaces part of the string with another substring. It only replaces the first occurrence by default.


3.7. split()

Splits a string into an array based on a specified separator.


3.8. trim()

Removes whitespace from both ends of a string.


3.9. includes()

Determines whether a string contains a specified substring, returning true or false.


4. Concatenation of Strings

You can join two or more strings together using either:

  • The + operator.
  • The concat() method.

Example:


end of part 2 -------->

Explore Other Chapters

Define Function -: