JavaScript string

JavaScript string

In JavaScript, a string is a sequence of characters enclosed in single or double quotes. To create a string variable, you can use the let or const keyword followed by the variable name, and then assign a string value to the variable using the = operator. For example:


let message = 'Hello, world!'; const greeting = "Hello, world!";         

You can also use the var keyword to create a string variable, but it is generally recommended to use let or const instead.

You can use the + operator to concatenate (join) two or more strings together. For example:


let firstName = 'John'; let lastName = 'Doe'; let fullName = firstName + ' ' + lastName; // "John Doe"         

You can also use template literals to create strings that contain expressions or variables. Template literals are enclosed in backticks () and use the ${expression}` syntax to include expressions or variables. For example:

let name = 'John'; let age = 30; let message = `Hello, my name is ${name} and I am ${age} years old.`; // "Hello, my name is John and I am 30 years old."         

You can use various string methods to manipulate strings, such as toUpperCase to convert a string to uppercase, toLowerCase to convert a string to lowercase, substring to extract a substring, and split to split a string into an array of substrings. For example:


let message = 'Hello, world!'; let uppercaseMessage = message.toUpperCase(); // "HELLO, WORLD!" let lowercaseMessage = message.toLowerCase(); // "hello, world!" let greeting = message.substring(0, 5); // "Hello" let words = message.split(' '); // ["Hello,", "world!"]         

You can store a string in a variable and access it later in your code. You can also perform various operations on strings, such as concatenation (combining two or more strings), slicing (extracting a part of a string), and formatting (replacing placeholders in a string with values).

Here are some examples of using variables with strings in JavaScript:



let firstName = 'John'; let lastName = 'Doe'; let fullName = firstName + ' ' + lastName; // "John Doe" let greeting = 'Hello, ' + fullName + '!'; // "Hello, John Doe!" let message = `Welcome to my website, ${fullName}!`; // "Welcome to my website, John Doe!"         

In the examples above, firstName, lastName, fullName, and greeting are all variables that store strings. The + operator is used to concatenate strings, and the `` (backtick) characters are used to define a template literal, which allows you to embed expressions (in this case, thefullName` variable) in a string.

To view or add a comment, sign in

More articles by Muhammad iqbal

  • AI: Karachi - AI Meetup DeepLearning.AI

    Pie & AI is a series of DeepLearning.AI #Meetups independently hosted by community groups.

  • How to add comments in python

    In Python, comments are used to provide explanations and notes about the code. Comments are ignored by the interpreter…

  • help() function in Python is used to display the documentation

    The help() function in Python is used to display the documentation or information about a specific module, function…

  • Python version check

    To check which version of Python is currently installed on your system, you can use the following command in the…

  • Introduction to Python

    Python is a high-level, interpreted programming language that is widely used for web development, scientific computing,…

  • how to create a dictionary in python ?

    In Python, a dictionary is a collection of key-value pairs. You can create a dictionary using the dict function, or by…

  • what is the difference between a list and a tuple in python ?

    In Python, a list is an ordered collection of objects that can be of any data type, including other lists. Lists are…

  • define a decorator in python ?

    In Python, a decorator is a design pattern that allows you to extend or modify the functionality of a class or function…

  • life cycle of containerised applications

    The life cycle of a containerised application generally consists of the following steps: Development: In this phase…

  • props in React

    Here are some key points about props in React: Props (short for "properties") are a way to pass data from a parent…

Insights from the community

Others also viewed

Explore topics