JavaScript Variables

Variable Definition Let's start by defining the variable. Variables are the names given to areas where the data necessary for the program to run is defined and kept in memory. If we compare memory cells, one by one or several of them, to a box, we can think of the variable name on the box and the value of the variable inside the box. As in the figure above, the box represents the variable name and the value inside represents the data held by the box.

JavaScript Data Types

There are 6 basic data types in JavaScript. When a JavaScript variable is defined, the variable is defined as one of these types.

Boolean: Represents the logical entity. Can contain two types of values: true and false Null: It is a data type that carries exactly the null value.

Undefined: If the variable is defined but no value is assigned, the value in the variable is undefined.

Number: According to EcmaScript standards, there is a single 64bit signed number type. Whole or decimal numbers are expressed with the number type. Additionally, there are 3 special symbolic values: +Infinity, -Infinity, and NaN (not-a-number).

String: It is the data type that JavaScript uses to represent textual data on a string. Each element in the string has a reference to a 16-bit unsigned integer. The index number of the first element in the string is 0. For other values ​​in the string, the index number continues to increase by 1. In summary: A textual expression is characterized as a string of characters.

Symbol (ECMAScript 6): It is a type defined by the EcmaScript 6 standard. Represents a unique and constant value for the object value. Object: It is the type used to keep the reference (path) of a specific object in memory. Arrays, Functions and Objects are expressed with this type.

JavaScript Variable Definition

In JavaScript, variables are defined with the var keyword. Variables are defined with the var statement for all data types. There are some basic rules to know when defining a variable in JavaScript. The variable must be defined before it is used. If a variable is defined within a scope, it can only be used within that scope. It can be used within the defined range. If a variable is assigned a value without using an identifier (var), this variable is defined globally and can be accessed from anywhere.


var variable;
var variable1="test";
var variable2, variable3, variable4=15;
var variable;
var variable1="test";
var variable2, variable3, variable4=15;
Variable Definition Rules Variable names are case sensitive. In other words, name and AD do not represent the same variable. Turkish characters should not be used in variable names. Variable names cannot contain special characters such as ?,!,:,%,- (except for the _ character). Special words in JavaScript language cannot be selected as variable names. Although it is not mandatory for variable names, lowercase letters are preferred. If the variable name consists of two or more words, the first letter of the words is capitalized except the first word. (number, salaryAmount, Number of books, etc.) Variable names cannot start with numbers.

var number=5;
var x, y=8, z;
var active = true;
var decimal= 5.4;
var value="hello";

var number=5;
var x, y=8, z;
var active = true;
var decimal= 5.4;
exists value="hello";
Using the Typeof Operator in JavaScript The typeof operator returns the data type of the specified variable or value as a string. The table below shows the results returned by the types with the typeof operator. Example:

// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof(42) === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // "Not-A-Number"
typeof Number(1) === 'number';

// String
typeof "" === 'string';
typeof "bla" === 'string';
typeof String("abc") === 'string';

// Boolean
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(true) === 'boolean';

// Symbols
typeof Symbol() === 'symbol'
typeof Symbol('foo') === 'symbol'
typeof Symbol.iterator === 'symbol'

// Undefined
typeof undefined === 'undefined';
typeof definedUnusedVariable=== 'undefined';
typeof undefinedVariable=== 'undefined';

// Objects
typeof {a:1} === 'object';
typeof [1, 2, 4] === 'object';

typeof new Date() === 'object';

typeof new Boolean(true) === 'object' ;
typeof new Number(1) === 'object';
typeof new String("abc") === 'object';

// Functions
typeof function(){} === 'function';
typeof class C {} === 'function';
typeof Math.sin === 'function';

// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof(42) === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // "Not-A-Number"
typeof Number(1) === 'number';

// String
typeof "" === 'string';
typeof "bla" === 'string';
typeof String("abc") === 'string';

// Boolean
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(true) === 'boolean';

// Symbols
typeof Symbol() === 'symbol'
typeof Symbol('foo') === 'symbol'
typeof Symbol.iterator === 'symbol'

// Undefined
typeof undefined === 'undefined';
typeof definedUnusedVariable=== 'undefined';
typeof undefinedVariable=== 'undefined';

// Objects
typeof {a:1} === 'object';
typeof [1, 2, 4] === 'object' ;

typeof new Date() === 'object';

typeof new Boolean(true) === 'object' ;
typeof new Number(1) === 'object';
typeof new String("abc") === 'object';

// Functions
typeof function(){} === 'function';
typeof class C {} === 'function';
typeof Math.sin === 'function';