Primitive values in JavaScript:

A.S.M.Hasan Sarker
5 min readMay 6, 2021

There are two types of data type in JavaScript. They are:
1. Primitive/ Value Types
2. Reference Types

1.Primitive / Value Types: In JavaScript, a primitive (primitive value, primitive data type) is not an object and has no methods. There are 7 primitive data types: string, number, bigint, boolean, undefined, symbol, and null.

A. String: We can declare a string in threeway.
i. let customerName = “John Doe”; // String Literal
ii. let firstName = String(‘John’); // Factory Function
iii. let lastName = new String(‘Doe’); // Constructor Function

B. Number: Number represents integer and floating numbers (decimals and exponentials). For example,

const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10⁵
A number type can also be +Infinity, -Infinity, and NaN (not a number). For example,

const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;
console.log(number2); // -Infinity

// strings can’t be divided by numbers
const number3 = “abc”/3;
console.log(number3); // NaN

C. BigInt: In JavaScript, Number type can only represent numbers less than (253–1) and more than -(253–1). However, if you need to use a larger number than that, you can use the BigInt data type.

A BigInt number is created by appending n to the end of an integer. For example,

// BigInt value
const value1 = 900719925124740998n;

// Adding two big integers
const result1 = value1 + 1n;
console.log(result1); // “900719925124740999n”

const value2 = 900719925124740998n;

// Error! BitInt and number cannot be added
const result2 = value2 + 1;
console.log(result2);

D. Boolean: This data type represents logical entities. Boolean represents one of two values: true or false. It is easier to think of it as a yes/no switch. For example,

const dataChecked = true;
const valueCounted = false;

E. undefined: The undefined data type represents value that is not assigned. If a variable is declared but the value is not assigned, then the value of that variable will be undefined. For example,

let name;
console.log(name); // undefined
It is also possible to explicitly assign a variable value undefined. For example,

let name = undefined;
console.log(name); // undefined

F. Symbol: This data type was introduced in a newer version of JavaScript (from ES2015).

A value having the data type Symbol can be referred to as a symbol value. The symbol is an immutable primitive value that is unique. For example,

// two symbols with the same description

const value1 = Symbol(‘hello’);
const value2 = Symbol(‘hello’);
Though value1 and value2 both contain ‘hello’, they are different from the Symbol type.

G. Null: In JavaScript, null is a special value that represents an empty or unknown value. For example,

const number = null;
The code above suggests that the number variable is empty.

Objects in JavaScript: JavaScript object is a non-primitive data type that allows you to store multiple data collections.

Note: If you are familiar with other programming languages, JavaScript objects are a bit different. You do not need to create classes to create objects.

Here is an example of a JavaScript object.

// object
const student = {
firstName: ‘ram’,
class: 10
};
Here, student is an object that stores values such as strings and numbers.

JavaScript Object Declaration:
The syntax to declare an object is:

const object_name = {
key1: value1,
key2: value2
}
Here, an object object_name is defined. Each member of an object is a key: value pair separated by commas and enclosed in curly braces {}.

For example,

// object creation
const person = {
name: ‘John’,
age: 20
};
console.log(typeof person); // object

JavaScript Function
A function is a block of code that performs a specific task.

Declaring a Function
The syntax to declare a function is:

function nameOfFunction () {
// function body
}
A function is declared using the function keyword.
The basic rules of naming a function are similar to naming a variable. It is better to write a descriptive name for your function. For example, if a function is used to add two numbers, you could name the function to add or addNumbers.
The body of a function is written within {}.
For example,

// declaring a function named greet()
function greet() {
console.log(“Hello there”);
}

Calling a Function
In the above program, we have declared a function named greet(). To use that function, we need to call it.

Here’s how you can call the above greet() function.

// function call
greet();

Expression: In general, an expression is a snippet of code that evaluates to a value. A statement is a snippet of code that acts. Wherever JavaScript expects a statement, you can write an expression. But the opposite isn’t true: if a framework or the JavaScript runtime expects an expression, you cannot use a statement.

The below code snippets are all expressions. They all evaluate to a value.

0 // 0

1 + 1 // 2

Typeof(): The typeof operator is used to get the data type (returns a string) of its operand. The operand can be either a literal or a data structure such as a variable, a function, or an object. The operator returns the data type.

Syntax

typeof operand
or
typeof (operand)
There are six possible values that typeof returns: object, boolean, function, number, string, and undefined. The following table summarizes possible values returned by the typeof operator.

Type of the operand Result
Object “object”
boolean “boolean”
function “function”
number “number”
string “string”
undefined “undefined”

Example: typeof(4+7); // returns number
typeof(“4”+”7"); // returns string

Comments in JavaScript: JavaScript comments can be used to explain JavaScript code, and to make it more readable.

JavaScript comments can also be used to prevent execution, when testing alternative code.

Single Line Comments
Single line comments start with //.

Any text between // and the end of the line will be ignored by JavaScript (will not be executed).

This example uses a single-line comment before each code line:

Example
// Change heading:
document.getElementById(“myH”).innerHTML = “My First Page”;

// Change paragraph:
document.getElementById(“myP”).innerHTML = “My first paragraph.”;

var x = 5; // Declare x, give it the value of 5
var y = x + 2; // Declare y, give it the value of x + 2

Multi-line Comments
Multi-line comments start with /* and end with */.

JavaScript will ignore any text between /* and */.

This example uses a multi-line comment (a comment block) to explain the code:

Example
/*
The code below will change
the heading with id = “myH”
and the paragraph with id = “myP”
in my web page:
*/
document.getElementById(“myH”).innerHTML = “My First Page”;
document.getElementById(“myP”).innerHTML = “My first paragraph.”;

--

--