0% found this document useful (0 votes)
3K views6 pages

Js My Notes

The document discusses JavaScript values, variables, data types, and parsing strings to numbers. It defines values and variables, describes primitive data types like numbers, strings, Booleans, and more. It also explains the parseInt and parseFloat functions for converting strings to integers and floating-point numbers respectively.

Uploaded by

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

Js My Notes

The document discusses JavaScript values, variables, data types, and parsing strings to numbers. It defines values and variables, describes primitive data types like numbers, strings, Booleans, and more. It also explains the parseInt and parseFloat functions for converting strings to integers and floating-point numbers respectively.

Uploaded by

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

JavaScript Notes

Topic

SHIVAM KUMAR RANA


* ====================================
* Values and Variable in JavaScript
* ====================================

* In JavaScript, values and variables are fundamental concepts that form the
basis of programming.

* Values: A value is a piece of information that a program can work with. It can
be a number, text, true/false, or more complex data.

* Variables: A variable is a container that holds a value. It has a name and can
be used to store and manipulate data in a program.

var myAge = 23;


console.log(myAge); o/p – 23;

! Let's test

var my_firstName = "John";


console.log(my_firstName); || true

var _myLastName$ = "Doe";

var 123myAge = 25;

var $cityName = "New York";

var my@Email = "[email protected]";


* ============================
* Data Types Section
* ============================

* Data types define the type of values that a variable can hold.

? Types of Primitive Data types

? Number: Represents numeric values, including integers and floating-point


numbers.
Example:
var myFavNo = 18;
console.log(myFavNo);

? String: Represents a sequence of characters enclosed in single or double


quotes.
Example:
var myName = "Shivam";
console.log(myName);

? Boolean: Represents a logical entity with two values: true or false.


Example:
var bool = true;
var boole = false;
console.log(bool+" "+boole);

? undefined: Represents the absence of a value or an uninitialized variable.


Example:
var shivam;
console.log(shivam);

? Null: Represents the absence of a value. It is often used to explicitly


indicate that a variable or object property has no assigned value.
Example:
var badMemory = null;
console.log(badMemory);

? BigInt: Represents integers of arbitrary precision (available since ECMAScript


2020).
Example:
const bigNumber = 1234567890123456789012345678901234567890n;

? Symbol: Represents a unique and immutable data type, often used to create
unique identifiers.
Example:
const mySymbol = Symbol("description");

! ==============================
! Data Types Interview Questions
! ==============================

? 1: What is the difference between null and undefined in JavaScript❓

In JavaScript, null and undefined are two distinct types that represent different values. By
definition, undefined means a variable has been declared but has not yet been assigned a
value, whereas null is an assignment value, meaning that a variable has been declared and
given the value of null .

? 2: What is the purpose of typeof operator in JavaScript❓

Typeof in JavaScript is an operator used for type checking and returns the data type of the
operand passed to it. The operand can be any variable, function, or object whose type you want
to find out using the typeof operator.

var myName = "shivam";


var myNo = "1";
console.log(myName);
console.log(typeof myName);
console.log(typeof myNo);

? 3: What is the result of `typeof null` in JavaScript❓

 var badMemory = null;


 console.log(badMemory);
 console.log(typeof badMemory); // o/p --> object(bug)

? 4: What are primitive data types in JavaScript❓

? 5: Convert a string to a number?


We just need to add the '+' sign before the string
Example:
var myFavNo = "10";

console.log(typeof myFavNo); o/p  string


console.log(typeof +myFavNo); o/p  number
console.log(typeof Number(myFavNo)); o/p  number

? 6: Convert a number to a string?


We just need to add an empty string after the number
Example:
let str = 5;

console.log(typeof str+""); o/p  number


console.log(typeof str); o/p  number
console.log(typeof String(str)); o/p  string
console.log(typeof (str+"")); o/p  string

? 7: Explain the concept of truthy and falsy values in JavaScript. Provide

examples.❓

Basically, if the variable value is false, zero, empty, null, undefined, or Nan , it's falsy and the
code within the if block is not run. If the variable value is anything else, such as a number that is
not zero, a non-empty string, an array, or an object, it's truthy and the code in the if block is run.

? 8: To check if a non-empty string is truthy or falsy in JavaScript, we can


directly use if statement.

========== Data Types End Section ==========

========== parseInt & parseFloat Section ==========

? parseInt and parseFloat are both functions in JavaScript used for converting
strings to numbers, but they have different use cases.

* parseInt: Definition: parseInt is used to parse a string and convert it to an


integer (whole number).
const myString = "42";
const myNumber = parseInt(myString, 10);
console.log(myNumber); // Output: 42

const myString = "42.5";


const myNumber = parseInt(myString);
console.log(myNumber); // Output: 42

* parseFloat: Definition: parseFloat is used to parse a string and convert it to


a floating-point number (decimal number).

const myString = "42.5";


const myNumber = parseFloat(myString);
console.log(myNumber); // Output: 42.5

TODO Key Differences:

? parseInt is used for converting to integers and ignores anything after the
decimal point.

? parseFloat is used for converting to floating-point numbers, preserving the


decimal part.

? Both functions will attempt to convert as much of the string as possible until
an invalid character is encountered.

//! Here are more examples


console.log(parseInt("123"));
// 123 (default base-10)
console.log(parseInt("123", 10));
// 123 (explicitly specify base-10)
console.log(parseInt(" 123 "));
// 123 (whitespace is ignored)
console.log(parseInt("077"));
// 77 (leading zeros are ignored)
console.log(parseInt("1.9"));
// 1 (decimal part is truncated)

//! When we will not get an Output


console.log(parseInt("&123"));
console.log(parseInt("-123"));
console.log(parseInt("xyz"));
// NaN (input can't be converted to an integer)

//? What is the purpose of the NaN value in JavaScript❓

========== parseInt & parseFloat End Section ==========

You might also like