DEV Community

Cover image for Mastering Functions: Unleashing the Power of Code
Karthick (k)
Karthick (k)

Posted on

Mastering Functions: Unleashing the Power of Code

Q1. Converting a string to Camel Case in JavaScript

CamelCase is a string formatting style in which the first word starts with a lowercase letter and each subsequent word starts with an uppercase letter. It is commonly used in JavaScript to name variables and functions for improved readability.

The first character of the string is converted to lowercase.
Each word after a space has its first character converted to uppercase, and spaces are removed.
This format helps create meaningful and readable variable or function names (e.g., myVariableName).

Functions in JavaScript

Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organise, reuse, and modularise code. It can take inputs, perform actions, and return outputs.

Return Statement

  • The return statement is used to send a result back from a function.

  • When the return executes, the function stops running at that point.

  • The returned value can be stored in a variable or used directly.

`function add(a, b) {
return a + b; // returns the sum
}

let result = add(5, 10);
console.log(result);`

JavaScript Hoisting

Hoisting refers to the behavior where JavaScript moves the declarations of variables, functions, and classes to the top of their scope during the compilation phase. This can sometimes lead to surprising results, especially when using var, let, const, or function expressions.

  • Hoisting applies to variable and function declarations.
  • Initialisations are not hoisted; they are only declarations.
  • 'var' variables are hoisted with undefined, while 'let' and 'const' are hoisted but remain in the Temporal Dead Zone until initialised.

Top comments (1)

Collapse
 
madhanraj profile image
Madhan Raj

Good Explanation with examples bro