DEV Community

Madhavan G
Madhavan G

Posted on

JavaScript Explained: From Basics to Data Types and Variables.

WHAT IS JAVASCRIPT?

•JavaScript (JS) is a lightweight interpreted (or just-in-time compiled) programming language with first-class functions.

•It is most well-known as the scripting language for Web pages.

•Many non-browser environments also use it, such as Node.js, Apache CouchDB and Adobe Acrobat.

•It is used for interact web application and supports both client side and server side development.

•Client side means code runs on the user's computer and server side means it's runs on the web browser.

•JavaScript is a single-threaded language that executes one task at a time.

⇒ What is Just-In-Time Compilation (JIT)
JIT (Just-In-Time Compilation) is a compilation process in which code is translated from an intermediate representation or a higher-level language (e.g., JavaScript or Java bytecode) into machine code at runtime, rather than prior to execution. This approach combines the benefits of both interpretation and ahead-of-time (AOT) compilation.

⇒ What is Scripting Language?
A scripting language is a programming language designed to write small programs (scripts) that automate tasks or control another program rather than building an entire standalone application.
Example:Javascript,PHP,Bash,Python,etc...

⇒ What is Programming Language?
A programming language is a formal language used to write instructions that a computer can execute, allowing developers to create software applications ranging from simple scripts to complex systems.
Example:Java,C,C#,C++,Rust,etc...


⇒What is datatypes in javascript?
•A data type tells the computer what kind of value a variable holds and what operations can be performed on it.

•In JS we have two main categories of Datatypes they are:
1.Primitive Data Type
2.Non-Primitive Data Type

⇒What is Primitive Data Type in javascript?

•A primitive data type is a basic, predefined data type provided by a programming language as a building block.

•In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties.

•Primitive Datatypes are immutable because we cannot change the value of a primitive in memory. You can only reassign the variable.

•Memory Location: They are typically stored in the stack (execution context) because they have a fixed size.

•When comparing primitives, JS checks the actual value, not the
reference.

•Now let see about it types:

Number:
*Its using to denote numbers like
Example:10,23,17...

Boolean:
*Boolean denoted by true or false.

Bigint:
*Normally integer values are number,on the other hand bigint means big numbers.
Ex:784645959265262.

String:
*Strings are set of characters enclosed single' 'or double""quotes.
Example:"welcome to my Blog"

Undefined:
*Undefined is variable can be exist but not value assigned

Null:
Null is denoted by empty value or 0 value.

Symbol:
*Used to create unique identifiers, usually for object property keys.
*Guarantees uniqueness: no two symbols are equal, even if they have the same description.
Example:

const id1 = Symbol("id");
const id2 = Symbol("id");

console.log(id1 === id2); // false → unique!
Enter fullscreen mode Exit fullscreen mode

⇒What is Non-Primitive Data Type in javascript?

•Non-primitive data types are complex, user-defined data types that store references (memory addresses) to objects, rather than the actual data values.

•Non-primitive data types are Mutable.

•Non-primitive types are stored in the heap the variable holds a reference (memory address) to the actual data. When you assign a non-primitive type to another variable, both variables point to the same memory location.

•Now let see about it types:

Object:

•Stores data as key-value pairs.
•Mutable — you can change properties without creating a new object.
Example:

const person = {
name: "Maddy",
age: 22,
};
console.log(person.name); // Maddy
person.age = 23;
console.log(person.age); // 23


Array:

•Ordered list of values.
•Mutable — you can add, remove, or change elements.
Example:

const numbers = [1, 2, 3, 4, 5];

numbers.push(6);

numbers[0] = 10;

console.log(numbers);


Function:

•Functions are objects in JavaScript.
•Can be stored in variables, passed around, and invoked.
Example:

function greet(name) {
return Hello, ${name}!;
}
console.log(greet("Maddy"));


Key Points:
⇒Primitive types are simple data types that store values directly. They are immutable, and each variable holds its own copy of the data.

⇒Non-primitive types are more complex, and variables store references to the data. Changes to one variable can affect others that reference the same data.


⇒what is Variables in Javascript?
•Variables are like containers that store information, such as numbers, text, or even complex data.

•In JavaScript, there are 4 ways to declare variables:
⇒const (Recommended)
⇒let (Recommended)
⇒var (Not Recommended)
⇒Automatically (Not Recommended)

•Each of these has its own rules.

const:
•Variables defined with const cannot be Redeclared.

•Variables defined with const cannot be Reassigned.

•Variables defined with const have Block Scope.

Example for const :
const n = 100;
// n = 200; This will throw an error
console.log(n)


let:
•The let keyword was introduced in ES6 (2015).

•Variables declared with let have Block Scope.

•Variables declared with let must be Declared before use.

•Variables declared with let cannot be Redeclared in the same scope.

Example for let :
let n= 10;
n = 20; // Value can be updated
// let n = 15; //can not redeclare
console.log(n)


var:
•Variables declared with the var always have Global Scope.

•Variables declared with the var keyword can NOT have block scope.

•Variables defined with var can be redeclared.

Example for var :
var n = 5;
console.log(n);

var n = 20; // reassigning is allowed
console.log(n);


Reference:https://developer.mozilla.org/en-US/docs/Web/JavaScript
Reference:https://www.geeksforgeeks.org/javascript/variables-datatypes-javascript/
Reference:https://www.w3schools.com/js/js_syntax.asp

Top comments (0)