0% found this document useful (0 votes)
0 views8 pages

Variable Js

The document provides an overview of variables in JavaScript, including their declaration, naming conventions, scope, and data types. It explains the differences between primitive and non-primitive data types, as well as the concepts of null and undefined. Additionally, it covers type conversion methods and the importance of using strict mode for better error handling.

Uploaded by

giridharkathi710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views8 pages

Variable Js

The document provides an overview of variables in JavaScript, including their declaration, naming conventions, scope, and data types. It explains the differences between primitive and non-primitive data types, as well as the concepts of null and undefined. Additionally, it covers type conversion methods and the importance of using strict mode for better error handling.

Uploaded by

giridharkathi710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 8

Variables

variables are the containers for storing data


values. Here are some key notes about a
variables:
1 Declaration: Variables in JavaScript are
declared using the var, let, or const keyword.
◦ var has been traditionally used but has some
scope-related issues.
◦ let is block-scoped and is preferable for
variable declaration.
◦ const is also block-scoped but its value cannot
be reassigned once it’s set.

//variable declared with var keyword followed by a name & assigned


a value using assignment operator
var x = 10;
let y = 20;
const PI = 3.14;

2 Naming Convention: Variable names in


JavaScript can contain letters, digits,
underscores, and dollar signs. They must begin
with a letter, underscore, or dollar sign.

var myVariable = 5;
var _myVariable = 10;
var $myVariable = 15;
3 Case Sensitivity: JavaScript variable names
are case-sensitive, meaning myVariable and
MyVariable are treated as different variables.

var myVariable = 5;
var MyVariable = 10;

console.log(myVariable); // Outputs: 5
console.log(MyVariable); // Outputs: 10

4 Data Types: JavaScript variables can hold


various data types including numbers, strings,
objects, arrays, functions, etc.
var num = 5;
var str = "Hello";
var arr = [1, 2, 3];
var obj = { name: "John", age: 30 };

5 Dynamic Typing: JavaScript is dynamically


typed, meaning you don't have to specify the
data type of a variable when declaring it. The
data type of the variable is determined
automatically at runtime.
6 Scope: Variables in JavaScript have function
or block scope, depending on how they are
declared.
◦ Variables declared with var are function-
scoped. They are accessible anywhere within
the function they are declared in.
◦ Variables declared with let or const are block-
scoped. They are only accessible within the
block they are declared in.
7 Hoisting: Variable declarations are hoisted to
the top of their scope during the compilation
phase, but their assignments remain where they
are.

myVar=5;
console.log(myVar); // Output: 5
var myVar; //----This declaraation is moved to top even before the
code execution—Hoisting

8 Global Variables: Variables declared


outside of any function have global scope and
can be accessed and modified from any part of
the script.
9 Local Variables: Variables declared within a
function have local scope and are only
accessible within that function.
10 Variable Reassignment: Variables
declared with var and let can be reassigned,
while variables declared with const cannot be
reassigned, though their properties can be
modified if they are objects.

let x = 5;
x = 10; // This is valid
11 Initialization: It’s recommended to initialize
variables when declaring them to avoid
unexpected behavior due to undefined
values.

x = 5;

12 Use of Strict Mode: Using "use strict";


at the beginning of a script or function enables
strict mode, which helps catch common coding
errors and prevents certain actions

Data types
1 Primitive Data Types:primitive data types
are the fundamental building blocks used to
represent single values. Primitive data types are
directly stored in memory and are immutable,
meaning their values cannot be changed after
they are created.
◦ Number: Represents numeric values,
including integers and floating-point numbers.
◦ String: Represents textual data, enclosed in
single or double quotes.
◦ Boolean: Represents a logical value, true or
false
◦ Undefined: Represents a variable that has
been declared but has not been assigned a
value.
◦ Null: Represents an intentional absence of any
object value.

2 Composite Data Types or Non


primitive:Non-primitive data types, also
known as reference types, are more complex
data structures that can hold multiple values and
have methods and properties.non-primitive data
types are mutable, meaning their values can be
changed after they are created, and they are
stored and accessed by reference rather than
by value.
◦ Object: Represents a collection of key-value
pairs, where keys are strings and values can be
any data type, including other objects.
◦ Array: Represents an ordered collection of
elements, accessed by index, starting from zero.
◦ Function: Represents a reusable block of
code that can be executed by invoking it.

Null & Undefined


1 null:
◦ Think of null as an intentional empty value. It's
used when you want to say, "This variable
intentionally has no value."
◦ For example, if you have a variable person but
don't yet know the person's details, you might
set person to null.
2 undefined:
◦ undefined means a variable has been
declared but hasn't been assigned a value yet.
◦ It's the default value of variables that haven't
been initialized.
◦ For example, if you declare a variable x but
don't assign any value to it, x is undefined by
default.

Type Conversion
Type coercion refers to the
automatic or implicit conversion of
values from one data type to
another. This process happens in the
background during operations
involving values of different types.
Types of Type Conversion
Implicit Conversion: This occurs
automatically when JavaScript
encounters an operation involving
different data types.
Explicit Conversion: This is when
you manually convert a value from
one type to another using functions
or methods.

Explicit Type Conversion


JavaScript type conversion, allowing
you to convert values from one data
type to another.
1 String(): Converts a value to a
string.
let num = 123; let str = String(num);
console.log(str); // Output: "123"
2 Number(): Converts a value to a
number.
let str = "123"; let num =
Number(str); console.log(num); //
Output: 123
3. Boolean(): Converts a value to a
boolean.
let num = 0; let bool =
Boolean(num); console.log(bool); //
Output: false

You might also like