Global JavaScript Methods

Global JS Methods

JavaScript global methods can be used on all JavaScript data types.

  • Number ( )
  • parseFloat ( )
  • parseInt ( )

Number ( )

The Number() function converts the object argument to a number that
represents the object’s value.
If the value cannot be converted to a legal number, NaN is returned.
If the parameter is a Date object, the Number() function returns the number of
milliseconds since midnight January 1, 1970, UTC.

Ex: –

Number(true)
Number(“100”)
Number(100/“Hello”)

parseInt ( )

The parseInt() function parses a string and returns an integer.

Syntax:- parseInt(string, radix)

The radix parameter is used to specify which numeral system to be used, for example, a
the radix of 16 (hexadecimal) indicates that the number in the string should be parsed from
a hexadecimal number to a decimal number.

If the radix parameter is omitted, JavaScript assumes the following:

  • If the string begins with “0x”, the radix is 16 (hexadecimal)
  • If the string begins with any other value, the radix is 10 (decimal)

Only the first number in the string is returned.
Leading and trailing spaces are allowed.
If the first character cannot be converted to a number, parseInt() returns NaN.

parseInt ( )

parseFloat ( )

The parseFloat() function parses a string and returns a floating-point number. This function determines if the first character in the specified string is a number. If it is, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string.

Syntax: – parseFloat(string)

  • Only the first number in the string is returned!
  • Leading and trailing spaces are allowed.
  • If the first character cannot be converted to a number, parseFloat() returns NaN.

parseFloat ( )

Tagged : / / / / /

How does JavaScript scope work?

Variable Scope

JavaScript has two scopes: –

  • Global
  • Local

Global Scope

A variable that is declared outside a function definition is a global variable, and its value is accessible and modifiable throughout your program. In a web browser, global variables are deleted when you close the browser window (or tab) but remain available to new pages loaded into the same window.

Local Scope

A variable that is declared inside a function definition is local. It is created and destroyed every time the function is executed, and it cannot be accessed by any code outside the function. Inside a function, if a variable has not been declared with var it is created as a global variable.

Local Scope

If there is a function inside a function the inner function can access outer function‟s variables but the outer function can not access inner function‟s variables. Function arguments (parameters) work as local variables inside functions.

Block Scope

Variables declared with var do not have block scope.

Block Scope

Identifiers declared with let and const do have block scope.

Variable Hoisting

Hoisting is JavaScript’s default behavior of moving declaration to the top of the function, if defined in a function, or the top of the global context, if outside a function.

Variable Hoisting

A variable can be used before it has been declared. Only variable declarations are hoisted to the top, not variable initialization.

Closure

A closure is a function having access to the parent scope. It preserves the data from the outside.

A closure is an inner function that has access to the outer (enclosing) function‟s variables.

For every closure we have three scopes:-

  • Local Scope ( Own scope)
  • Outer Functions Scope
  • Global Scope
Tagged : / / /

PHP Global and Local Variable

In this tutorial we will learn about declaring global and local variable in PHP.

Initially we will see what is local variable and global variable with examples.

Here if we see the variable is declared inside the function “local” so it will work inside the local function only until we return the value inside the function.

In this we can see that the variable “a” is declared outside the function local, so it won’t work inside the function until we call it.

If we see the output for this we will see an error since “a” is a local variable and it will not work beyond the function “test”.

Now in this code if we see, variable “a” is declared globally and we will get an error if we call it inside any function.

To execute the global variable in the function we need to use predefined variable “global” inside the function.

We will see now that it will return value inside the function as well as outside the function.

References:

Tagged : / / /