JavaScript Values:
JavaScript defines two types of values:
- Fixed values or Literals
- Variable values or Variables
Literals
Literals represent values in the JavaScript program. These are constant values – not variables – embedded or provided directly in the JavaScript. For example, Numeric Literals – numbers with or without decimals:
7.50 – floating-point literal
10 – integer literal
7.50 – floating-point literal
10 – integer literal
Strings Literals – text within double/single quotes:
"Broto"
'Broto'
"Broto"
'Broto'
Boolean Literals – allow only two values:
true: it represents a real boolean value
false: it represents a false boolean value
true: it represents a real boolean value
false: it represents a false boolean value
Object Literals – list of pairs of property names and associated values of objects, enclosed in {}. For example: {name: 'Broto', age: 40}
Array Literals – a list of values, enclosed in []. For example: [7, 8, 19, 5]
Variables
Variables are used to store values. A variable is a container or storage area to hold data such as a number, string, object, and array. There are two different types of variables in JavaScript: let and const.
A variable must be identified with a unique name. This unique name is called an identifier, like myName. To create a variable in JavaScript, we have to declare a variable. JavaScript uses the let or the const keyword to declare variables. Storing a value in a variable is called initialization. The initialize assigns a value with an equal sign to the variable.
Naming Variables In JavaScript
In JavaScript, some rules are to be followed while naming variables/identifiers:
1. Variable names must start with a letter, an underscore character, or a dollar sign. It cannot start with a numeral (0-9). For example:
name, _name, name007, or $name – a valid variable name.
007name – an invalid one.
2. Variable names cannot contain spaces. For instance:
myName, my_name, name007 – valid variable name.
my Name, my name, name 007 – invalid variable name.
3. Variable names cannot be the same as reserved keywords such as const, if, continue, true, or false.
4. When variables are named with more than one word, we can use several techniques such as Camel Case (myName), Pascal Case (MyName), or Snake Case (my_name).
By convention, the JavaScript variable name is written in camelCase.
5. Variable name is case sensitive in JavaScript. For example, the word myName is not the same as myname.
Variable Declaration In JavaScript
Variables should always be declared before they are used. A variable declared without the let/const keyword is called an undeclared variable. It creates a Global variable (a variable that we can use from any other code in the current document, including within functions). For example:
{
x = 2; // x is not declared, so it is a global variable.
}
console.log(x); // 2, we can call outside from block.
A variable declared without an initial value will have the value undefined. For example:
let x;
console.log(x); // undefined
When to use let and const in JS:
If you think the value of the variable can change, use let, otherwise use const. Use const when you declare a new Array, a new Object, or a new Function.
Using the const keyword, you can add or/and change the elements of an array or add and change the properties of an object. For example:
const colors = ['Blue','Red','Green'];
console.log(colors); // ['Blue', 'Red', 'Green']
// can add en element
colors.push('Pink');
console.log(colors); // ['Blue', 'Purple', 'Green', 'Pink']
// can change en element
colors[1]='Purple';
console.log(colors); // ['Blue', 'Purple', 'Green']
JavaScript Data Types:
Data types specify what kind of data can be stored and manipulated within a JS program. JavaScript is a dynamically or loosely typed language. This means that we don’t have to specify the data type of a variable when you declare the variable. In JavaScript, data types are automatically converted as needed during script execution. For example:
let data; // data is of undefined type
data = 30; // data is of integer type
data = 'Broto'; // data is of string type
There are two types of data types in JavaScript:
Primitive data types – contain only a single value. They are immutable. That means they do not have methods or properties that can alter them.
Non-primitive/Reference/Object data types – key-value pairs of data. Objects can be of any length and do not have a fixed size. They are mutated. That means that certain methods can alter them.
There are seven primitive data types:
1. Number data type – An integer or floating point number. For example:
let x = 5; // integer datatype
let y = 5.5; // floating datatype
2. String data type – textual data. For example:
let myName = 'Broto';
3. Boolean data type – true or false. For example:
let isStudent = false;
4. BigInt data type – cannot have decimals. For example:
let z = BigInt ('123456789102345n');
5. Null data type – represents an absence of a value, empty value, no value, or nothing. It is inhabited by exactly one value null. For example:
let a = null;
6. Undefined – if a variable is declared, but not assigned a value. For example:
let b;
7. Symbol – used for a unique identifier. For example:
let c1 = Symble();
Non-primitive/Reference/Object data types can contain:
An object data type – to store key: value data collections. For example:
const myInfo = {
name = 'Broto',
age = 30
}
An array data type – to store ordered data collections. We can store a number, string, and other data types in one variable. For example:
const myColor = ['blue', 'red', 'green', 'purple'];
const myInfo = [['Broto', 35], 'Danish'];
A date Object – to store the information about dates and times. It is an object because JavaScript does not have a date data type. For example:
const date = new Date('2023-10-20');
An error Object – to store the information about an error. For example:
ReferenceError, SyntaxError, TypeError, and so on.
0 Comments