Javascript Comments, Variables, and Data Type
JavaScript is one of the most versatile and widely-used programming languages in the world. Whether you're building interactive websites, creating dynamic applications, or diving into server-side programming, understanding JavaScript fundamentals is key to becoming a proficient developer. In this blog, we’ll explore the essentials of JavaScript, including how to use comments, declare variables, and work with different data types. Let's start this exciting journey into the world of JavaScript!
1. JavaScript Comments
Comments are used in JavaScript to make the code more readable and maintainable by explaining what specific parts of the code do. They are ignored during execution.
Types of Comments
1. Single-line comments
-
Begin with
//
. - Used for brief explanations or notes.
// This is a single-line comment
let name = "John"; // Variable to store the name
2. Multi-line comments
- Begin with
/*
and end with*/
. - Useful for longer explanations or temporarily disabling blocks of code.
/* This is a multi-line comment.
It can span multiple lines. */
let age = 30;
2. JavaScript Variables
Variables are containers that store data. In JavaScript, you can declare
variables using let
, const
, or var
.
Variable Declaration
-
let
- Introduced in ES6.
- Block-scoped (only accessible within the block they are declared in).
- Can be reassigned.
let age = 25; // Declare a variable age = 30; // Reassign the variable
-
const
- Introduced in ES6.
- Block-scoped.
- Cannot be reassigned after its initial declaration.
const PI = 3.14; // Declare a constant // PI = 3.14159; // Error: Cannot reassign a constant
-
var
- Function-scoped (not block-scoped).
- Can be reassigned.
- Avoid using
var
in modern JavaScript aslet
andconst
provide better scoping rules.
var isAvailable = true; // Declare a variable isAvailable = false; // Reassign the variable
Variable Naming Rules
- Must start with a letter,
$
, or_
. - Cannot contain spaces or special characters (except
$
and_
). - Cannot be a reserved keyword.
3. JavaScript Data Types
JavaScript is a loosely-typed (dynamic) language, meaning variables do not have a fixed type. You can assign different types of values to the same variable.
Primitive Data Types
-
String
- Used for textual data.
- Enclosed in single (
'
) or double ("
) quotes.
let greeting = "Hello, World!";
-
Number
- Represents integers or floating-point numbers.
let age = 25; // Integer let price = 19.99; // Float
-
Bigint
- JavaScript BigInt is a new datatype (ES2020) that can be used to store integer values that are too big to be represented by a normal JavaScript Number.
let x = BigInt("78987465874123654789564795");
-
Boolean
- Represents a logical value: true or false.
let isActive = true; // Boolean value
-
Undefined
- Represents a variable that has been declared but not assigned a value.
let x; console.log(x); // Output: undefined
-
Null
- Represents an intentional absence of any object value.
let user = null; // Null value
-
Symbol
- Represents an intentional absence of any object value.
let user = null; // Null value
-
Object
- The object data type can contain both built-in objects, and user defined objects:
Built-in object types can be:
objects, arrays, dates, maps, sets, intarrays, floatarrays, promises, and more.
- The object data type can contain both built-in objects, and user defined objects:
Post a Comment
0 Comments