If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

Storing strings in variables

AAP‑1 (EU)
AAP‑1.A.3 (EK)
AAP‑1.C (LO)
AAP‑1.C.3 (EK)
AAP‑1.C.4 (EK)
When we're making a program, we often want to store sequences of letters, like storing “easy” as a game's difficulty level. In programming lingo, we call each letter a character and we call the sequence a string.
Here are examples of storing strings in JavaScript:
var player = "GrayFox";
var storeName = "Baskin Robbins 31 Flavors";
var grade = "A+";
📝 See similar code in: App Lab | Snap | Python
A string can contain any number of characters, including none at all, and those characters can be letters, numbers, or symbols.
Notice how each string is surrounded by double quotes on either side. If you don’t put any quotes around a string, JavaScript will get confused.
🔍 What happens if you forget the quotes? See for yourself!
Uh oh, error!
Here's what happens on that first line: JavaScript looks for a variable named GrayFox. If it could actually find one, it would set the player variable equal to the value of the GrayFox variable. Since it doesn't find one, it errors instead. If you want it to store the literal string, you must surround it in quotes.
✏️ See if you can fix the errors in the program above.
Check your understanding
Which of these are valid lines of JavaScript to store a string?
Choose all answers that apply:
Choose all answers that apply:

Pseudocode for strings

This pseudocode represents storing a string:
a ← "STRING"
Whenever you see that pseudocode, it means that the variable a (or whatever it's called) is storing the string inside the double quotes.
For example, you might see pseudocode like this:
difficulty ← "medium"
That means the variable difficulty is storing the string "medium".
Let's look at the equivalent code in a few textual languages:
JavaScriptvar difficulty = "medium";
Pythondifficulty = "medium"
JavaString difficulty = "medium";
Check your understanding
What is the pseudocode equivalent of this JavaScript?
var hair = "curly";
Choose 1 answer:
Choose 1 answer:

🙋🏽🙋🏻‍♀️🙋🏿‍♂️Do you have any questions about this topic? We'd love to answer— just ask in the questions area below!

Want to join the conversation?

  • starky ultimate style avatar for user sebastianbrinezc
    Is possible to get an overflow error in a string variable? I mean like overflow error in number variable.
    (1 vote)
    Default Khan Academy avatar avatar for user
    • aqualine ultimate style avatar for user Martin
      Not like numbers precisely. With numbers you have space and an encoding: For example, say you have 4 bits and you only encode positive numbers in binary:
      Then you can encode all numbers between 0 and 15 (0000 and 1111), if you use that system for something like 8 + 9 you get overflow because 10001 cannot be represented with the for bits. So you might end up with 8 + 9 = 2 (0010).

      Strings are stored as arrays, with each field in the array holding a character.

      With higher-level languages that isn't an issue, with javascript, for example, you just tell your compiler you want a string and you don't have to do anything else on lower languages though you'll have to do that yourself.

      So Khan would be stored in an array like so
      K|a|h|n or rather the binary representation of those letters (K = 75 = 1001011 and so on)
      Now depending on how much space you allocated for the array holding the String you might get overflow, that means you're telling the system to write characters to somewhere that wasn't originally assigned that function.
      (e.g. you have an array with length 10 and you write something like array[20] = 'a').

      That can cause unexpected behaviour and create security vulnerabilities in your code (vulnerability to buffer overflow attacks).

      Unexpected behaviour means something happens, but you don't really know what.

      Hope that helps and it isn't too long.
      (4 votes)
  • duskpin sapling style avatar for user Weather
    How do you store quotation marks in strings if they're part of the syntax, and how are line breaks made?
    (2 votes)
    Default Khan Academy avatar avatar for user
  • aqualine sapling style avatar for user TheCoder99
    If I wanted to create a variable in pseudocode,
    would I do it like this:
    curly <- 10

    or this:
    curly -> 10
    (1 vote)
    Default Khan Academy avatar avatar for user
  • piceratops ultimate style avatar for user SIDTHEKID12345
    How do you change the String to key.code and back again
    (1 vote)
    Default Khan Academy avatar avatar for user
  • piceratops seed style avatar for user Henrik Van Tassell
    I'm curious why these examples are using var in JS as opposed to let.
    (1 vote)
    Default Khan Academy avatar avatar for user
    • starky ultimate style avatar for user KLaudano
      The article might use "var" to be consistent with other articles/videos on Khan Academy that were created prior to the addition of "let" to the JS language in 2015. Of course, that is just a guess; I cannot say for certain.
      (1 vote)
  • duskpin seed style avatar for user Yunisha
    Hello, I have have just started backusing khan academy,such a great app to learn to code and all the information. My question is if I decide to write this code in pseudocode. Do I still have to use a semicolon after the quotation marks?

    Example: var skittles <= "blue";
    (0 votes)
    Default Khan Academy avatar avatar for user
    • hopper jumping style avatar for user pamela ❤
      From the author:This pseudocode is the pseudocode style that is used by the AP CSP exam and it does not use semi-colons. If you aren't taking the AP CSP exam, I'd suggest learning JavaScript from our Intro to JS course instead- it's more interactive than these articles!
      (3 votes)
  • aqualine ultimate style avatar for user axel_gomez
    bruh -alan romero
    (1 vote)
    Default Khan Academy avatar avatar for user
  • spunky sam blue style avatar for user ℭ++ [Javar]
    Is there a way to execute code that is in a string? Like:
    "function myFunction () {}" or "if(a){println(b);}"
    (0 votes)
    Default Khan Academy avatar avatar for user
    • starky ultimate style avatar for user KLaudano
      Yes, you can use the eval function to run code stored as a string in JavaScript (e.g. x = eval("3 + 5")). However, YOU SHOULD NOT USE THIS FUNCTION! It is an enormous security risk and regarded as a terrible idea to use. Khan Academy does not even let you use this function in your projects.
      (1 vote)
  • piceratops ultimate style avatar for user Sederboy
    How do you remove a single character from a string variable?
    (0 votes)
    Default Khan Academy avatar avatar for user
  • leaf red style avatar for user layaz7717
    Is there anyway to store a string as the sum as two variables? For example, what if var firstName = "Spongebob" and var lastName = "Squarepants". Could I store var fullName (which would be Spongebob Squarepants) as the combination of two variables?
    (0 votes)
    Default Khan Academy avatar avatar for user
    • leaf green style avatar for user Shane McGookey
      Yes, you can. The concept that you are looking for is "string concatenation." You can concatenate (append one string to the end of another) two strings using the '+' operator.

      Using the example you provided above, you would have:

      let fullName = firstName + ' ' + lastName;

      When evaluated, fullName would be "Spongebob Squarepants".
      (0 votes)