0% found this document useful (0 votes)
5 views

String

Uploaded by

Rakesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

String

Uploaded by

Rakesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

How to execute JavaScript?

 JavaScript can be executed right inside one’s browser. You


can open the JS console and start writing JS there.
 Another way to execute JS is a runtime like Node.js which
can be installed and used to run JavaScript code.
 Yet another way to execute JavaScript is by inserting it
inside the <script> tag of an HTML document.

What is JavaScript?
JavaScript is a programming language that is commonly used in
web development. It is a client-side language, which means
that it is executed by the user's web browser rather than the
web server. This allows JavaScript to interact with the user and
create dynamic, interactive web pages.
JavaScript is often used in combination with HTML and CSS to
create web pages that are more interactive and engaging. It
can be used to create all sorts of effects, such as drop-down
menus, image sliders, and pop-up windows.
What are Variables?
In JavaScript, variables are used to store data. They are an
essential part of any programming language, as they allow you
to store, retrieve, and manipulate data in your programs.
There are a few different ways to declare variables in
JavaScript, each with its own syntax and rules.
Declaring Variables
To declare a variable in JavaScript, you use the "var" keyword
followed by the name of the variable. For example:

var x;

This declares a variable called "x" but does not assign any
value to it. You can also assign a value to a variable when you
declare it, like this:

var x = 10;
In JavaScript, you can also use the "let" and "const" keywords
to declare variables. The "let" keyword is used to declare a
variable that can be reassigned later, while the "const" keyword
is used to declare a variable that cannot be reassigned. For
example:

let y = 20;
const z = 30;

In this example, "y" is a variable that can be reassigned, while


"z" is a constant that cannot be reassigned.
Data Types
In JavaScript, there are several data types that you can use to
store different types of data. Some common data types include:
 Numbers (e.g. 10, 3.14)
 Strings (e.g. "hello", 'world')
 Booleans (e.g. true, false)
 Arrays (e.g. [1, 2, 3])
 Objects (e.g. { name: "John", age: 30 })
Variable Naming Rules
JavaScript is a dynamically-typed language, which means that
you don't have to specify the data type of a variable when you
declare it. The data type of a variable is determined by the
value that is assigned to it. For example:

var x = 10; // x is a number


var y = "hello"; // y is a string
var z = [1, 2, 3]; // z is an array

Variable Naming Rules


There are a few rules that you need to follow when naming
variables in JavaScript:
 Variable names can only contain letters, digits,
underscores, and dollar signs.
 Variable names cannot start with a digit.
 Variable names are case-sensitive.
It is also a good practice to use descriptive and meaningful
names for your variables, as this makes your code easier to
read and understand.
Using Variables
Once you have declared a variable, you can use it to store and
retrieve data in your program. For example:

var x = 10;
console.log(x); // prints 10

x = "hello";
console.log(x); // prints "hello"

You can also perform various operations on variables, such as


mathematical calculations, string concatenation, and more. For
example:

var x = 10;
var y = 20;
var z = x + y; // z is 30

var str1 = "hello";


var str2 = "world";
var str3 = str1 + " " + str2; // str3 is "hello world"

Strings
One of the most important aspects of JavaScript is its ability to
manipulate strings, which are sequences of characters. The
basics of JavaScript strings and the various string methods that
can be used to manipulate them.
A string in JavaScript is a sequence of characters enclosed in
either single or double quotes. For example, the following are
valid strings in JavaScript:

"Hello World"
'Hello World'

JavaScript provides a number of built-in methods for


manipulating strings. Some of the most commonly used string
methods are:
length - This method returns the number of characters in a
string. For example, the following code will return 11:

var str = "Hello World";


console.log(str.length);

concat - This method is used to concatenate (combine) two or


more strings. For example, the following code will return "Hello
World":

var str1 = "Hello";


var str2 = " World";
console.log(str1.concat(str2));

indexOf - This method is used to find the index of a specific


character or substring in a string. For example, the following
code will return 6:

var str = "Hello World";


console.log(str.indexOf("W"));

slice - This method is used to extract a portion of a string. For


example, the following code will return "World":

var str = "Hello World";


console.log(str.slice(6));

replace - This method is used to replace a specific character


or substring in a string. For example, the following code will
return "Hello Universe":

var str = "Hello World";


console.log(str.replace("World", "Universe"));

toUpperCase and toLowerCase - These methods are used to


convert a string to uppercase or lowercase letters. For example,
the following code will return "HELLO WORLD" and "hello world"
respectively:

var str = "Hello World";


console.log(str.toUpperCase());
console.log(str.toLowerCase());

These are just a few of the many string methods available in


JavaScript. By understanding the basics of strings and the
various methods that can be used to manipulate them, you can
create more dynamic and interactive web pages. So, start
experimenting with different string methods and see what you
can create!

You might also like