0% found this document useful (0 votes)
2 views23 pages

Js Programming 07-06-2024

The document provides an overview of JavaScript programming, covering key concepts such as data types, comments, statements, keywords, variable declaration, and naming rules. It explains the use of operators, arrays, objects, and functions, along with their syntax and importance in code reusability. Additionally, it discusses string handling and methods, emphasizing the distinction between primitive values and objects.

Uploaded by

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

Js Programming 07-06-2024

The document provides an overview of JavaScript programming, covering key concepts such as data types, comments, statements, keywords, variable declaration, and naming rules. It explains the use of operators, arrays, objects, and functions, along with their syntax and importance in code reusability. Additionally, it discusses string handling and methods, emphasizing the distinction between primitive values and objects.

Uploaded by

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

Javascript Programming

NITHIN
[email protected]
Hello world
console.log()
Datatypes in JavaScript
• Number (200, 3.14, …)
• String (“Hello world”, “Pentagon Space”, ‘some string’, …)

• Boolean (true, false)


• BigInt (1234567890123456789012345678901234567890n)
• Symbol (Symbol(‘anySymbolDescription'))
• Null (null)
• Undefined (variable has not been initialized yet)
• Object (representation of real world entities)
Comments in JavaScript
Readability, explanation, prevent execution, testing alternate code

• Single line comment


// I am a single line comment
• Multi line comment
/*
I am a
multi-line
comment
*/
Statements
Composed of values, operators, expressions, keywords, and comments.

Program -> set of instructions


Each instruction -> statement

A JS program is simply a list of JS statements

Statements are terminated using (;) a semicolon.


But is a semicolon mandatory? (Nah)
White Spaces
Javascript ignores whitespaces. You can add them to improve code
readability.

Friendly advice -> Spaces around operators

Eg:
let person="Hege";
let person = "Hege"; (5 marks for good handwriting? I wish)
Keywords
Reserved words

Examples –
var return
let else
const while
if do
switch try
for (end of thinking capacity)
function
JavaScript Values
• Fixed values (Literals) – numbers and strings
• Dynamic values (Variables) – used to store data values

Declaring variables
JS uses the keywords var, let and const to declare variables
(var –> ES5, let & const -> ES6)
(var -> function scoped, let & const -> block scoped
(let -> can be reassigned, const -> cannot be reassigned)
Rules for naming identifiers
What is an identifier?

Constitution of identifiers –
1. Can include uppercase & lowercase letters, digits, (_) and
($) special characters.
2. Cannot begin with a digit.
3. Keywords cannot be used as identifiers
4. Whitespaces cannot be used while naming identifiers

NOTE – JS is case-sensitive (myVar and myvar are two different


variables)
Casing
Snake case (first_name)
Pascal case (First_Name)
Camel case (firstName)  preferred

Remarks
Use const if the value of the variable should remain unchanged.
Use let where the value of the variable keeps changing.
Only use var if you must support old browsers.
var, let and const
Additional points
* const must be initialized
* Variables declared using var are hoisted (usage before declaration)
* Prefer const for arrays, objects, functions, etc.
Operators
Arithmetic Operators (+, -, *, /, %, **, ++, --)
Assignment Operators (=, +=, -=, *=, /=, %=, **=)
Comparison Operators (==, ===, !=, !==, >, >=, <, <=, ?:)
Logical Operators (&&, ||, !)
Type Operators (typeof, instanceof)
Bitwise Operators (&, |, ~, <<, >>)

Unary Operators
Binary Operators
Ternary Operators

Operator Precedence
Arrays
Enclosed within square brackets
Separated by commas
Indexes are zero-based (i.e., starting from 0)

const cars = ["Saab", "Volvo", "BMW"];

Objects
Enclosed within curly braces {}
Separated by commas
Key-value pairs

const person = {firstName:"John", lastName:"Doe", age:50,


eyeColor:"blue"};
Functions
Block of code designed to perform a particular task
A function is executed when something “invokes” (calls) it.

Eg:

function myFunction(p1, p2) {


return p1 * p2;
}

Function names need to abide by the same rules as naming variables.


Functions Extended
Syntax:

function name(parameter1, parameter2, parameter3) {


// code to be executed
}

Parameters are listed inside the parenthesis in the function defn.


Arguments are the values received by the function when it was
invoked.
Inside the function, parameters behave as local variables.
More on functions
When JS reaches a return statement, the function stops execution.
The returned value is given back to the caller (the place where the
function was invoked).
The () operator invokes functions
Functions can be assigned to variables

Why functions
Reusability
Reduce in redundant code
Dynamic code (different arguments provide different results)
Objects again
// Create an Object
const person = new Object();

// Add Properties
person.firstName = "John";
person.lastName = "Doe";
person.age = 50;
person.eyeColor = "blue";

Methods are actions that can be performed on objects. Methods are


function definitions stored as property values.
Objects with methods
const person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};

Here, this refers to the person object.


Objects can have both methods, and properties
Everything is an object? 
Objects are objects
Functions are objects
Dates are objects
Arrays are objects
Maps are objects
Sets are objects

Basically, all values in JavaScript, except primitives, are objects.

Primitive values are immutable. Objects are mutable. (addressed by


reference, not by value)
Strings
Strings are for storing text. Strings are enclosed within quotes. (can
be single or double quotes).
Backticks (`) can also be used for storing strings. (template strings)

A JavaScript string is zero or more characters written inside quotes.


let carName1 = "Volvo XC60"; // Double quotes
let carName2 = ‘Volvo XC60'; // Single quotes

NOTE –
Strings created with single or double quotes works the same.
There is no difference between the two.
More on strings
Templates are not supported in Internet Explorer.

To find the length of a string, use the built-in ‘length’ property.


Eg –
let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = text.length;

Escape characters:
String methods
.charAt(position)
.at(position)
.slice(start, end) [end index is not included]
.substring(start, end)
.indexOf(charSequence)
.includes(charSequence) [case-sensitive, ES6]
.startsWith(phrase, startPos)
.endsWith(phrase, endPos) [check if first endPos letters have phrase]
.toLowerCase()
.toUpperCase()
.split(separator) [string to array of strings separated by a
separator]

You might also like