Data Types in JavaScript

Post a Comment


Hello to everyone. We will all learn about data types in JavaScript programming on this occasion. The set of data types that a programming language supports is one of its most fundamental characteristics. It is a value type that can be represented and manipulated using a programming language. JavaScript supports the following primitive data types:

  • Numbersfor example. 123, 120.50 etc.
  • Stringse.g. "this text string" etc.
  • Booleane.g. true or false.

JavaScript also defines two trivial data types, null and undefined, each of which defines only a single value. In addition to these primitive data types, JavaScript supports a composite data type known as an object.

Note: JavaScript does not differentiate between integer and floating-point values. In JavaScript, all numbers are represented as floating-point values. JavaScript uses the IEEE 754 standard's 64-bit floating-point format to represent numbers.

JavaScript Variables

Variables are used in JavaScript, as they are in many other programming languages. Variables can be viewed as named containers.

We can store data in this container and then refer to it by naming the container. Variables must be declared before they can be used in JavaScript programs. Variables are declared with keywords in the following order.

We can also declare multiple variables with the same keyword varlike the following -

Storing a value in a variable is called variable initialization. We can initialize the variable at the time of variable creation or at a later time when we need the variable.

For example, we can create a named variable and assign a value of 2000.50 to it later. For other variables, we can set the value at initialization as follows.

Note - Use keyword vari-e only for declaration or initialization, once in a lifetime variable name in the document. We should not re-declare the same variable twice.

JavaScript is an untyped language. This means that JavaScript variables can store values ​​of any data type.

Unlike many other languages, we don't need to tell JavaScript during variable declaration what type of value the variable will hold.

The value type of a variable can change during the execution of a program, and JavaScript takes care of it automatically.

JavaScript Variable Scope

The scope of the variable is the area of the program in which it is defined. JavaScript variables have only two scopes.

  • Variabel Global: Global variables have global scope, which means they can be defined anywhere in your JavaScript code.

  • Variabel Lokal: A local variable will be visible only in the function in which it is defined. Function parameters are always local to that function.

Local variables with the same name take precedence over global variables with the same name within a function. When we declare a local variable or function parameter with the same name as a global variable, the global variable is effectively hidden. Consider the following example.

This produces the following results:

local

JavaScript Variable Name

When naming our variables in JavaScript, observe the following rules:

  • We must not use JavaScript-provided keywords as variable names. These keywords are mentioned in the next section. For example, the variable name breakor booleaninvalid"

  • JavaScript variable names can not start with angka (0–9). They must start with a letter or underscore character. For example, tes 123 is an invalid variable name, but _123test is a valid name.

  • JavaScript variable names are case sensitive. For example, "name" and "name" are two different variables.

source: https://www.tutorialspoint.com/javascript/javascript_variables.htm

Related Posts

Post a Comment

Subscribe Our Newsletter